Remove old test verification logic
[scons.git] / bench / is_types.py
blobb47c3812f24f5eac696026a79ffee9096e9a8b1d
1 # __COPYRIGHT__
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.
7 import types
8 try:
9 from collections import UserDict, UserList, UserString
10 except ImportError:
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
17 DictType = dict
18 ListType = list
19 StringType = str
20 try: unicode
21 except NameError:
22 UnicodeType = None
23 else:
24 UnicodeType = unicode
27 # The original implementations, pretty straightforward checks for the
28 # type of the object and whether it's an instance of the corresponding
29 # User* type.
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))
40 else:
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*
48 # type.
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))
63 else:
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):
74 t = type(e)
75 return t is dict or \
76 (t is types.InstanceType and isinstance(e, UserDict))
78 def cache_type_e_is_List(e):
79 t = type(e)
80 return t is list \
81 or (t is types.InstanceType and isinstance(e, UserList))
83 if UnicodeType is not None:
84 def cache_type_e_is_String(e):
85 t = type(e)
86 return t is str \
87 or t is unicode \
88 or (t is types.InstanceType and isinstance(e, UserString))
89 else:
90 def cache_type_e_is_String(e):
91 t = type(e)
92 return t is str \
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):
102 t = type(e)
103 return t is DictType or \
104 (t is InstanceType and isinstance(e, UserDict))
106 def global_cache_type_e_is_List(e):
107 t = type(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):
113 t = type(e)
114 return t is StringType \
115 or t is UnicodeType \
116 or (t is InstanceType and isinstance(e, UserString))
117 else:
118 def global_cache_type_e_is_String(e):
119 t = type(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.
128 instanceTypeMap = {
129 UserDict : dict,
130 UserList : list,
131 UserString : str,
134 if UnicodeType is not None:
135 def myType(obj):
136 t = type(obj)
137 if t is types.InstanceType:
138 t = instanceTypeMap.get(obj.__class__, t)
139 elif t is unicode:
140 t = str
141 return t
142 else:
143 def myType(obj):
144 t = type(obj)
145 if t is types.InstanceType:
146 t = instanceTypeMap.get(obj.__class__, t)
147 return 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
161 def Func01(obj):
162 """original_is_String"""
163 for i in IterationList:
164 original_is_String(obj)
166 def Func02(obj):
167 """original_is_List"""
168 for i in IterationList:
169 original_is_List(obj)
171 def Func03(obj):
172 """original_is_Dict"""
173 for i in IterationList:
174 original_is_Dict(obj)
176 def Func04(obj):
177 """checkInstanceType_is_String"""
178 for i in IterationList:
179 checkInstanceType_is_String(obj)
181 def Func05(obj):
182 """checkInstanceType_is_List"""
183 for i in IterationList:
184 checkInstanceType_is_List(obj)
186 def Func06(obj):
187 """checkInstanceType_is_Dict"""
188 for i in IterationList:
189 checkInstanceType_is_Dict(obj)
191 def Func07(obj):
192 """cache_type_e_is_String"""
193 for i in IterationList:
194 cache_type_e_is_String(obj)
196 def Func08(obj):
197 """cache_type_e_is_List"""
198 for i in IterationList:
199 cache_type_e_is_List(obj)
201 def Func09(obj):
202 """cache_type_e_is_Dict"""
203 for i in IterationList:
204 cache_type_e_is_Dict(obj)
206 def Func10(obj):
207 """global_cache_type_e_is_String"""
208 for i in IterationList:
209 global_cache_type_e_is_String(obj)
211 def Func11(obj):
212 """global_cache_type_e_is_List"""
213 for i in IterationList:
214 global_cache_type_e_is_List(obj)
216 def Func12(obj):
217 """global_cache_type_e_is_Dict"""
218 for i in IterationList:
219 global_cache_type_e_is_Dict(obj)
221 #def Func13(obj):
222 # """myType_is_String"""
223 # for i in IterationList:
224 # myType_is_String(obj)
226 #def Func14(obj):
227 # """myType_is_List"""
228 # for i in IterationList:
229 # myType_is_List(obj)
231 #def Func15(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'},
245 # ),
247 class A:
248 pass
250 Data = [
252 "String",
253 ('',),
257 "List",
258 ([],),
262 "Dict",
263 ({},),
267 "UserString",
268 (UserString(''),),
272 "UserList",
273 (UserList([]),),
277 "UserDict",
278 (UserDict({}),),
282 "Object",
283 (A(),),
288 # Local Variables:
289 # tab-width:4
290 # indent-tabs-mode:nil
291 # End:
292 # vim: set expandtab tabstop=4 shiftwidth=4: