Merge branch 'master' into AddOption-option-obj
[scons.git] / bench / is_types.py
blobecc0edbdb067f9788ac48fd8fbbaeb6e6f4454dc
1 # __COPYRIGHT__
3 # Benchmarks for testing various possible implementations
4 # of the is_Dict(), is_List() and is_String() functions in
5 # SCons/Util.py.
7 import types
8 from collections import UserDict, UserList, UserString, deque
9 from collections.abc import Iterable, MappingView, MutableMapping, MutableSequence
11 DictType = dict
12 ListType = list
13 StringType = str
16 # The original implementations, pretty straightforward checks for the
17 # type of the object and whether it's an instance of the corresponding
18 # User* type.
20 def original_is_Dict(e):
21 return isinstance(e, (dict, UserDict))
23 def original_is_List(e):
24 return isinstance(e, (list, UserList))
26 def original_is_String(e):
27 return isinstance(e, (str, UserString))
30 # New candidates that explicitly check for whether the object is an
31 # InstanceType before calling isinstance() on the corresponding User*
32 # type. Update: meaningless in Python 3, InstanceType was only for
33 # old-style classes, so these are just removed.
34 # XXX
36 # New candidates that try more generic names from collections:
38 def new_is_Dict(e):
39 return isinstance(e, MutableMapping)
41 def new_is_List(e):
42 return isinstance(e, MutableSequence)
44 def new_is_String(e):
45 return isinstance(e, (str, UserString))
47 # Improved candidates that cache the type(e) result in a variable
48 # before doing any checks.
50 def cache_type_e_is_Dict(e):
51 t = type(e)
52 return t is dict or isinstance(e, UserDict)
54 def cache_type_e_is_List(e):
55 t = type(e)
56 return t is list or isinstance(e, (UserList, deque))
58 def cache_type_e_is_String(e):
59 t = type(e)
60 return t is str or isinstance(e, UserString)
63 # Improved candidates that cache the type(e) result in a variable
64 # before doing any checks, but using the global names for
65 # DictType, ListType and StringType.
67 def global_cache_type_e_is_Dict(e):
68 t = type(e)
69 return t is DictType or isinstance(e, UserDict)
71 def global_cache_type_e_is_List(e):
72 t = type(e)
73 return t is ListType or isinstance(e, (UserList, deque))
75 def global_cache_type_e_is_String(e):
76 t = type(e)
77 return t is StringType or isinstance(e, UserString)
80 # Alternative that uses a myType() function to map the User* objects
81 # to their corresponding underlying types.
82 # Again, since this used InstanceType, it's not useful for Python 3.
85 # These are the actual test entry points
87 def Func01(obj):
88 """original_is_String"""
89 for i in IterationList:
90 original_is_String(obj)
92 def Func02(obj):
93 """original_is_List"""
94 for i in IterationList:
95 original_is_List(obj)
97 def Func03(obj):
98 """original_is_Dict"""
99 for i in IterationList:
100 original_is_Dict(obj)
102 def Func04(obj):
103 """new_is_String"""
104 for i in IterationList:
105 new_is_String(obj)
107 def Func05(obj):
108 """new_is_List"""
109 for i in IterationList:
110 new_is_List(obj)
112 def Func06(obj):
113 """new_is_Dict"""
114 for i in IterationList:
115 new_is_Dict(obj)
117 def Func07(obj):
118 """cache_type_e_is_String"""
119 for i in IterationList:
120 cache_type_e_is_String(obj)
122 def Func08(obj):
123 """cache_type_e_is_List"""
124 for i in IterationList:
125 cache_type_e_is_List(obj)
127 def Func09(obj):
128 """cache_type_e_is_Dict"""
129 for i in IterationList:
130 cache_type_e_is_Dict(obj)
132 def Func10(obj):
133 """global_cache_type_e_is_String"""
134 for i in IterationList:
135 global_cache_type_e_is_String(obj)
137 def Func11(obj):
138 """global_cache_type_e_is_List"""
139 for i in IterationList:
140 global_cache_type_e_is_List(obj)
142 def Func12(obj):
143 """global_cache_type_e_is_Dict"""
144 for i in IterationList:
145 global_cache_type_e_is_Dict(obj)
148 # Data to pass to the functions on each run. Each entry is a
149 # three-element tuple:
152 # "Label to print describing this data run",
153 # ('positional', 'arguments'),
154 # {'keyword' : 'arguments'},
155 # ),
157 class A:
158 pass
160 Data = [
162 "String",
163 ('',),
167 "List",
168 ([],),
172 "Dict",
173 ({},),
177 "UserString",
178 (UserString(''),),
182 "UserList",
183 (UserList([]),),
187 "deque",
188 (deque([]),),
192 "UserDict",
193 (UserDict({}),),
197 "Object",
198 (A(),),
203 # Local Variables:
204 # tab-width:4
205 # indent-tabs-mode:nil
206 # End:
207 # vim: set expandtab tabstop=4 shiftwidth=4: