3 # Benchmarks for testing various possible implementations
4 # of the is_Dict(), is_List() and is_String() functions in
8 from collections
import UserDict
, UserList
, UserString
, deque
9 from collections
.abc
import Iterable
, MappingView
, MutableMapping
, MutableSequence
16 # The original implementations, pretty straightforward checks for the
17 # type of the object and whether it's an instance of the corresponding
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.
36 # New candidates that try more generic names from collections:
39 return isinstance(e
, MutableMapping
)
42 return isinstance(e
, MutableSequence
)
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
):
52 return t
is dict or isinstance(e
, UserDict
)
54 def cache_type_e_is_List(e
):
56 return t
is list or isinstance(e
, (UserList
, deque
))
58 def cache_type_e_is_String(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
):
69 return t
is DictType
or isinstance(e
, UserDict
)
71 def global_cache_type_e_is_List(e
):
73 return t
is ListType
or isinstance(e
, (UserList
, deque
))
75 def global_cache_type_e_is_String(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
88 """original_is_String"""
89 for i
in IterationList
:
90 original_is_String(obj
)
93 """original_is_List"""
94 for i
in IterationList
:
98 """original_is_Dict"""
99 for i
in IterationList
:
100 original_is_Dict(obj
)
104 for i
in IterationList
:
109 for i
in IterationList
:
114 for i
in IterationList
:
118 """cache_type_e_is_String"""
119 for i
in IterationList
:
120 cache_type_e_is_String(obj
)
123 """cache_type_e_is_List"""
124 for i
in IterationList
:
125 cache_type_e_is_List(obj
)
128 """cache_type_e_is_Dict"""
129 for i
in IterationList
:
130 cache_type_e_is_Dict(obj
)
133 """global_cache_type_e_is_String"""
134 for i
in IterationList
:
135 global_cache_type_e_is_String(obj
)
138 """global_cache_type_e_is_List"""
139 for i
in IterationList
:
140 global_cache_type_e_is_List(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'},
205 # indent-tabs-mode:nil
207 # vim: set expandtab tabstop=4 shiftwidth=4: