3 # Benchmarks for testing various possible implementations
4 # of the is_Dict(), is_List() and is_String() functions in
5 # src/engine/SCons/Util.py.
8 from collections
import UserDict
, UserList
, UserString
15 # The original implementations, pretty straightforward checks for the
16 # type of the object and whether it's an instance of the corresponding
19 def original_is_Dict(e
):
20 return isinstance(e
, (dict, UserDict
))
22 def original_is_List(e
):
23 return isinstance(e
, (list, UserList
))
25 def original_is_String(e
):
26 return isinstance(e
, (str, UserString
))
29 # New candidates that explicitly check for whether the object is an
30 # InstanceType before calling isinstance() on the corresponding User*
32 # InstanceType was only for old-style classes, so absent in Python 3
33 # this this is no different than the previous
35 def checkInstanceType_is_Dict(e
):
36 return isinstance(e
, (dict, UserDict
))
38 def checkInstanceType_is_List(e
):
39 return isinstance(e
, (list, UserList
))
41 def checkInstanceType_is_String(e
):
42 return isinstance(e
, (str, UserString
))
45 # Improved candidates that cache the type(e) result in a variable
46 # before doing any checks.
48 def cache_type_e_is_Dict(e
):
50 return t
is dict or isinstance(e
, UserDict
)
52 def cache_type_e_is_List(e
):
54 return t
is list or isinstance(e
, UserList
)
56 def cache_type_e_is_String(e
):
58 return t
is str or isinstance(e
, UserString
)
61 # Improved candidates that cache the type(e) result in a variable
62 # before doing any checks, but using the global names for
63 # DictType, ListType and StringType.
65 def global_cache_type_e_is_Dict(e
):
67 return t
is DictType
or isinstance(e
, UserDict
)
69 def global_cache_type_e_is_List(e
):
71 return t
is ListType
or isinstance(e
, UserList
)
73 def global_cache_type_e_is_String(e
):
75 return t
is StringType
or isinstance(e
, UserString
)
78 # Alternative that uses a myType() function to map the User* objects
79 # to their corresponding underlying types.
89 if t
is types
.InstanceType
:
90 t
= instanceTypeMap
.get(obj
.__class
__, t
)
93 def myType_is_Dict(e
):
94 return myType(e
) is dict
96 def myType_is_List(e
):
97 return myType(e
) is list
99 def myType_is_String(e
):
100 return myType(e
) is str
106 """original_is_String"""
107 for i
in IterationList
:
108 original_is_String(obj
)
111 """original_is_List"""
112 for i
in IterationList
:
113 original_is_List(obj
)
116 """original_is_Dict"""
117 for i
in IterationList
:
118 original_is_Dict(obj
)
121 """checkInstanceType_is_String"""
122 for i
in IterationList
:
123 checkInstanceType_is_String(obj
)
126 """checkInstanceType_is_List"""
127 for i
in IterationList
:
128 checkInstanceType_is_List(obj
)
131 """checkInstanceType_is_Dict"""
132 for i
in IterationList
:
133 checkInstanceType_is_Dict(obj
)
136 """cache_type_e_is_String"""
137 for i
in IterationList
:
138 cache_type_e_is_String(obj
)
141 """cache_type_e_is_List"""
142 for i
in IterationList
:
143 cache_type_e_is_List(obj
)
146 """cache_type_e_is_Dict"""
147 for i
in IterationList
:
148 cache_type_e_is_Dict(obj
)
151 """global_cache_type_e_is_String"""
152 for i
in IterationList
:
153 global_cache_type_e_is_String(obj
)
156 """global_cache_type_e_is_List"""
157 for i
in IterationList
:
158 global_cache_type_e_is_List(obj
)
161 """global_cache_type_e_is_Dict"""
162 for i
in IterationList
:
163 global_cache_type_e_is_Dict(obj
)
166 # """myType_is_String"""
167 # for i in IterationList:
168 # myType_is_String(obj)
171 # """myType_is_List"""
172 # for i in IterationList:
173 # myType_is_List(obj)
176 # """myType_is_Dict"""
177 # for i in IterationList:
178 # myType_is_Dict(obj)
182 # Data to pass to the functions on each run. Each entry is a
183 # three-element tuple:
186 # "Label to print describing this data run",
187 # ('positional', 'arguments'),
188 # {'keyword' : 'arguments'},
234 # indent-tabs-mode:nil
236 # vim: set expandtab tabstop=4 shiftwidth=4: