minimize tool initialization in tests
[scons.git] / bench / is_types.py
blob01db768ca9e0929f4563e2b0917af777352aaf57
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 from collections import UserDict, UserList, UserString
10 DictType = dict
11 ListType = list
12 StringType = str
15 # The original implementations, pretty straightforward checks for the
16 # type of the object and whether it's an instance of the corresponding
17 # User* type.
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*
31 # type.
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):
49 t = type(e)
50 return t is dict or isinstance(e, UserDict)
52 def cache_type_e_is_List(e):
53 t = type(e)
54 return t is list or isinstance(e, UserList)
56 def cache_type_e_is_String(e):
57 t = type(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):
66 t = type(e)
67 return t is DictType or isinstance(e, UserDict)
69 def global_cache_type_e_is_List(e):
70 t = type(e)
71 return t is ListType or isinstance(e, UserList)
73 def global_cache_type_e_is_String(e):
74 t = type(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.
81 instanceTypeMap = {
82 UserDict : dict,
83 UserList : list,
84 UserString : str,
87 def myType(obj):
88 t = type(obj)
89 if t is types.InstanceType:
90 t = instanceTypeMap.get(obj.__class__, t)
91 return 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
105 def Func01(obj):
106 """original_is_String"""
107 for i in IterationList:
108 original_is_String(obj)
110 def Func02(obj):
111 """original_is_List"""
112 for i in IterationList:
113 original_is_List(obj)
115 def Func03(obj):
116 """original_is_Dict"""
117 for i in IterationList:
118 original_is_Dict(obj)
120 def Func04(obj):
121 """checkInstanceType_is_String"""
122 for i in IterationList:
123 checkInstanceType_is_String(obj)
125 def Func05(obj):
126 """checkInstanceType_is_List"""
127 for i in IterationList:
128 checkInstanceType_is_List(obj)
130 def Func06(obj):
131 """checkInstanceType_is_Dict"""
132 for i in IterationList:
133 checkInstanceType_is_Dict(obj)
135 def Func07(obj):
136 """cache_type_e_is_String"""
137 for i in IterationList:
138 cache_type_e_is_String(obj)
140 def Func08(obj):
141 """cache_type_e_is_List"""
142 for i in IterationList:
143 cache_type_e_is_List(obj)
145 def Func09(obj):
146 """cache_type_e_is_Dict"""
147 for i in IterationList:
148 cache_type_e_is_Dict(obj)
150 def Func10(obj):
151 """global_cache_type_e_is_String"""
152 for i in IterationList:
153 global_cache_type_e_is_String(obj)
155 def Func11(obj):
156 """global_cache_type_e_is_List"""
157 for i in IterationList:
158 global_cache_type_e_is_List(obj)
160 def Func12(obj):
161 """global_cache_type_e_is_Dict"""
162 for i in IterationList:
163 global_cache_type_e_is_Dict(obj)
165 #def Func13(obj):
166 # """myType_is_String"""
167 # for i in IterationList:
168 # myType_is_String(obj)
170 #def Func14(obj):
171 # """myType_is_List"""
172 # for i in IterationList:
173 # myType_is_List(obj)
175 #def Func15(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'},
189 # ),
191 class A:
192 pass
194 Data = [
196 "String",
197 ('',),
201 "List",
202 ([],),
206 "Dict",
207 ({},),
211 "UserString",
212 (UserString(''),),
216 "UserList",
217 (UserList([]),),
221 "UserDict",
222 (UserDict({}),),
226 "Object",
227 (A(),),
232 # Local Variables:
233 # tab-width:4
234 # indent-tabs-mode:nil
235 # End:
236 # vim: set expandtab tabstop=4 shiftwidth=4: