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 UserDict import UserDict
9 from UserList import UserList
12 from UserString import UserString
14 # "Borrowed" from the Python 2.2 UserString module
15 # and modified slightly for use with SCons.
17 def __init__(self, seq):
18 if isinstance(seq, str):
20 elif isinstance(seq, UserString):
21 self.data = seq.data[:]
24 def __str__(self): return str(self.data)
25 def __repr__(self): return repr(self.data)
26 def __int__(self): return int(self.data)
27 def __long__(self): return long(self.data)
28 def __float__(self): return float(self.data)
29 def __complex__(self): return complex(self.data)
30 def __hash__(self): return hash(self.data)
33 if isinstance(s, UserString):
34 return cmp(self.data, s.data)
36 return cmp(self.data, s)
37 def __contains__(self, char):
38 return char in self.data
40 def __len__(self): return len(self.data)
41 def __getitem__(self, index): return self.__class__(self.data[index])
42 def __getslice__(self, start, end):
43 start = max(start, 0); end = max(end, 0)
44 return self.__class__(self.data[start:end])
46 def __add__(self, other):
47 if isinstance(other, UserString):
48 return self.__class__(self.data + other.data)
49 elif is_String(other):
50 return self.__class__(self.data + other)
52 return self.__class__(self.data + str(other))
53 def __radd__(self, other):
55 return self.__class__(other + self.data)
57 return self.__class__(str(other) + self.data)
59 return self.__class__(self.data*n)
62 InstanceType = types.InstanceType
73 # The original implementations, pretty straightforward checks for the
74 # type of the object and whether it's an instance of the corresponding
77 def original_is_Dict(e):
78 return isinstance(e, dict) or isinstance(e, UserDict)
80 def original_is_List(e):
81 return isinstance(e, list) or isinstance(e, UserList)
83 if UnicodeType is not None:
84 def original_is_String(e):
85 return isinstance(e, str) \
86 or isinstance(e, unicode) \
87 or isinstance(e, UserString)
89 def original_is_String(e):
90 return isinstance(e, str) or isinstance(e, UserString)
94 # New candidates that explicitly check for whether the object is an
95 # InstanceType before calling isinstance() on the corresponding User*
98 def checkInstanceType_is_Dict(e):
99 return isinstance(e, dict) or \
100 (isinstance(e, types.InstanceType) and isinstance(e, UserDict))
102 def checkInstanceType_is_List(e):
103 return isinstance(e, list) \
104 or (isinstance(e, types.InstanceType) and isinstance(e, UserList))
106 if UnicodeType is not None:
107 def checkInstanceType_is_String(e):
108 return isinstance(e, str) \
109 or isinstance(e, unicode) \
110 or (isinstance(e, types.InstanceType) and isinstance(e, UserString))
112 def checkInstanceType_is_String(e):
113 return isinstance(e, str) \
114 or (isinstance(e, types.InstanceType) and isinstance(e, UserString))
118 # Improved candidates that cache the type(e) result in a variable
119 # before doing any checks.
121 def cache_type_e_is_Dict(e):
123 return t is dict or \
124 (t is types.InstanceType and isinstance(e, UserDict))
126 def cache_type_e_is_List(e):
129 or (t is types.InstanceType and isinstance(e, UserList))
131 if UnicodeType is not None:
132 def cache_type_e_is_String(e):
136 or (t is types.InstanceType and isinstance(e, UserString))
138 def cache_type_e_is_String(e):
141 or (t is types.InstanceType and isinstance(e, UserString))
145 # Improved candidates that cache the type(e) result in a variable
146 # before doing any checks, but using the global names for
147 # DictType, ListType and StringType.
149 def global_cache_type_e_is_Dict(e):
151 return t is DictType or \
152 (t is InstanceType and isinstance(e, UserDict))
154 def global_cache_type_e_is_List(e):
156 return t is ListType \
157 or (t is InstanceType and isinstance(e, UserList))
159 if UnicodeType is not None:
160 def global_cache_type_e_is_String(e):
162 return t is StringType \
163 or t is UnicodeType \
164 or (t is InstanceType and isinstance(e, UserString))
166 def global_cache_type_e_is_String(e):
168 return t is StringType \
169 or (t is InstanceType and isinstance(e, UserString))
173 # Alternative that uses a myType() function to map the User* objects
174 # to their corresponding underlying types.
182 if UnicodeType is not None:
185 if t is types.InstanceType:
186 t = instanceTypeMap.get(obj.__class__, t)
193 if t is types.InstanceType:
194 t = instanceTypeMap.get(obj.__class__, t)
197 def myType_is_Dict(e):
198 return myType(e) is dict
200 def myType_is_List(e):
201 return myType(e) is list
203 def myType_is_String(e):
204 return myType(e) is str
210 """original_is_String"""
211 for i in IterationList:
212 original_is_String(obj)
215 """original_is_List"""
216 for i in IterationList:
217 original_is_List(obj)
220 """original_is_Dict"""
221 for i in IterationList:
222 original_is_Dict(obj)
225 """checkInstanceType_is_String"""
226 for i in IterationList:
227 checkInstanceType_is_String(obj)
230 """checkInstanceType_is_List"""
231 for i in IterationList:
232 checkInstanceType_is_List(obj)
235 """checkInstanceType_is_Dict"""
236 for i in IterationList:
237 checkInstanceType_is_Dict(obj)
240 """cache_type_e_is_String"""
241 for i in IterationList:
242 cache_type_e_is_String(obj)
245 """cache_type_e_is_List"""
246 for i in IterationList:
247 cache_type_e_is_List(obj)
250 """cache_type_e_is_Dict"""
251 for i in IterationList:
252 cache_type_e_is_Dict(obj)
255 """global_cache_type_e_is_String"""
256 for i in IterationList:
257 global_cache_type_e_is_String(obj)
260 """global_cache_type_e_is_List"""
261 for i in IterationList:
262 global_cache_type_e_is_List(obj)
265 """global_cache_type_e_is_Dict"""
266 for i in IterationList:
267 global_cache_type_e_is_Dict(obj)
270 # """myType_is_String"""
271 # for i in IterationList:
272 # myType_is_String(obj)
275 # """myType_is_List"""
276 # for i in IterationList:
277 # myType_is_List(obj)
280 # """myType_is_Dict"""
281 # for i in IterationList:
282 # myType_is_Dict(obj)
286 # Data to pass to the functions on each run. Each entry is a
287 # three-element tuple:
290 # "Label to print describing this data run",
291 # ('positional', 'arguments'),
292 # {'keyword' : 'arguments'},
338 # indent-tabs-mode:nil
340 # vim: set expandtab tabstop=4 shiftwidth=4: