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.
9 from collections
import UserDict
, UserList
, UserString
11 # No 'collections' module or no UserFoo in collections
12 exec('from UserDict import UserDict')
13 exec('from UserList import UserList')
14 exec('from UserString import UserString')
16 InstanceType
= types
.InstanceType
27 # The original implementations, pretty straightforward checks for the
28 # type of the object and whether it's an instance of the corresponding
31 def original_is_Dict(e
):
32 return isinstance(e
, (dict,UserDict
))
34 def original_is_List(e
):
35 return isinstance(e
, (list,UserList
))
37 if UnicodeType
is not None:
38 def original_is_String(e
):
39 return isinstance(e
, (str,unicode,UserString
))
41 def original_is_String(e
):
42 return isinstance(e
, (str,UserString
))
46 # New candidates that explicitly check for whether the object is an
47 # InstanceType before calling isinstance() on the corresponding User*
50 def checkInstanceType_is_Dict(e
):
51 return isinstance(e
, dict) or \
52 (isinstance(e
, types
.InstanceType
) and isinstance(e
, UserDict
))
54 def checkInstanceType_is_List(e
):
55 return isinstance(e
, list) \
56 or (isinstance(e
, types
.InstanceType
) and isinstance(e
, UserList
))
58 if UnicodeType
is not None:
59 def checkInstanceType_is_String(e
):
60 return isinstance(e
, str) \
61 or isinstance(e
, unicode) \
62 or (isinstance(e
, types
.InstanceType
) and isinstance(e
, UserString
))
64 def checkInstanceType_is_String(e
):
65 return isinstance(e
, str) \
66 or (isinstance(e
, types
.InstanceType
) and isinstance(e
, UserString
))
70 # Improved candidates that cache the type(e) result in a variable
71 # before doing any checks.
73 def cache_type_e_is_Dict(e
):
76 (t
is types
.InstanceType
and isinstance(e
, UserDict
))
78 def cache_type_e_is_List(e
):
81 or (t
is types
.InstanceType
and isinstance(e
, UserList
))
83 if UnicodeType
is not None:
84 def cache_type_e_is_String(e
):
88 or (t
is types
.InstanceType
and isinstance(e
, UserString
))
90 def cache_type_e_is_String(e
):
93 or (t
is types
.InstanceType
and isinstance(e
, UserString
))
97 # Improved candidates that cache the type(e) result in a variable
98 # before doing any checks, but using the global names for
99 # DictType, ListType and StringType.
101 def global_cache_type_e_is_Dict(e
):
103 return t
is DictType
or \
104 (t
is InstanceType
and isinstance(e
, UserDict
))
106 def global_cache_type_e_is_List(e
):
108 return t
is ListType \
109 or (t
is InstanceType
and isinstance(e
, UserList
))
111 if UnicodeType
is not None:
112 def global_cache_type_e_is_String(e
):
114 return t
is StringType \
115 or t
is UnicodeType \
116 or (t
is InstanceType
and isinstance(e
, UserString
))
118 def global_cache_type_e_is_String(e
):
120 return t
is StringType \
121 or (t
is InstanceType
and isinstance(e
, UserString
))
125 # Alternative that uses a myType() function to map the User* objects
126 # to their corresponding underlying types.
134 if UnicodeType
is not None:
137 if t
is types
.InstanceType
:
138 t
= instanceTypeMap
.get(obj
.__class
__, t
)
145 if t
is types
.InstanceType
:
146 t
= instanceTypeMap
.get(obj
.__class
__, t
)
149 def myType_is_Dict(e
):
150 return myType(e
) is dict
152 def myType_is_List(e
):
153 return myType(e
) is list
155 def myType_is_String(e
):
156 return myType(e
) is str
162 """original_is_String"""
163 for i
in IterationList
:
164 original_is_String(obj
)
167 """original_is_List"""
168 for i
in IterationList
:
169 original_is_List(obj
)
172 """original_is_Dict"""
173 for i
in IterationList
:
174 original_is_Dict(obj
)
177 """checkInstanceType_is_String"""
178 for i
in IterationList
:
179 checkInstanceType_is_String(obj
)
182 """checkInstanceType_is_List"""
183 for i
in IterationList
:
184 checkInstanceType_is_List(obj
)
187 """checkInstanceType_is_Dict"""
188 for i
in IterationList
:
189 checkInstanceType_is_Dict(obj
)
192 """cache_type_e_is_String"""
193 for i
in IterationList
:
194 cache_type_e_is_String(obj
)
197 """cache_type_e_is_List"""
198 for i
in IterationList
:
199 cache_type_e_is_List(obj
)
202 """cache_type_e_is_Dict"""
203 for i
in IterationList
:
204 cache_type_e_is_Dict(obj
)
207 """global_cache_type_e_is_String"""
208 for i
in IterationList
:
209 global_cache_type_e_is_String(obj
)
212 """global_cache_type_e_is_List"""
213 for i
in IterationList
:
214 global_cache_type_e_is_List(obj
)
217 """global_cache_type_e_is_Dict"""
218 for i
in IterationList
:
219 global_cache_type_e_is_Dict(obj
)
222 # """myType_is_String"""
223 # for i in IterationList:
224 # myType_is_String(obj)
227 # """myType_is_List"""
228 # for i in IterationList:
229 # myType_is_List(obj)
232 # """myType_is_Dict"""
233 # for i in IterationList:
234 # myType_is_Dict(obj)
238 # Data to pass to the functions on each run. Each entry is a
239 # three-element tuple:
242 # "Label to print describing this data run",
243 # ('positional', 'arguments'),
244 # {'keyword' : 'arguments'},
290 # indent-tabs-mode:nil
292 # vim: set expandtab tabstop=4 shiftwidth=4: