Created
September 30, 2024 17:27
-
-
Save nitzmahone/c035228b9a4ad5a032319c8b5e131cb5 to your computer and use it in GitHub Desktop.
Type instance micro-benchmark comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TL;DR: use isinstance for subclass-friendly type checks unless microperf is needed, then use type is or type in (set) as needed | |
Timings on 3.11-3.13 | |
3.11: | |
wrong: 0.01907933602342382s | |
isinstance: 0.03639550699153915s | |
isinstance (multiple types tuple): 0.0790955779957585s | |
in set: 0.05437163001624867s | |
in tuple: 0.07667801500065252s | |
any set: 0.39473058498697355s | |
any tuple: 0.38263560098130256s | |
3.12 | |
wrong: 0.020254609000403434s | |
isinstance: 0.04267390904715285s | |
isinstance (multiple types tuple): 0.08680826798081398s | |
in set: 0.0511601020116359s | |
in tuple: 0.07611375296255574s | |
any set: 0.4684364930144511s | |
any tuple: 0.4442763639963232s | |
3.13 | |
wrong: 0.02352760697249323s | |
isinstance: 0.03476412297459319s | |
isinstance (multiple types tuple): 0.07270792697090656s | |
in set: 0.044357359991408885s | |
in tuple: 0.06767950300127268s | |
any set: 0.4882963169948198s | |
any tuple: 0.423168417008128s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from ansible.template.jinja_bits import AnsibleUndefined, DeferredErrorMarker | |
from ansible.template.templar import TemplateContext, Templar | |
from ansible.utils.datatag.tags import TrustedAsTemplate | |
class AnotherOne(DeferredErrorMarker): | |
"""bleh""" | |
tc = TemplateContext(template_value="blah", templar=Templar(), options=None, stop_on_template=False).__enter__() | |
undefined_types = {AnsibleUndefined, DeferredErrorMarker, AnotherOne} | |
undefined_types_tuple = tuple(undefined_types) | |
value = TrustedAsTemplate().tag('hello') # AnotherOne(exception=Exception()) | |
def in_undefined_set(v): | |
return type(value) in undefined_types | |
def in_undefined_tuple(v): | |
return type(value) in undefined_types_tuple | |
def any_undefined_set(v): | |
value_type = type(value) | |
return any(value_type is t for t in undefined_types) | |
def any_undefined_tuple(v): | |
value_type = type(value) | |
return any(value_type is t for t in undefined_types_tuple) | |
print(f"wrong: {timeit.timeit('type(value) is AnsibleUndefined', globals=globals())}s") | |
print(f"isinstance: {timeit.timeit('isinstance(value, AnsibleUndefined)', globals=globals())}s") | |
print(f"isinstance (multiple types tuple): {timeit.timeit('isinstance(value, undefined_types_tuple)', globals=globals())}s") | |
print(f"in set: {timeit.timeit('in_undefined_set(value)', globals=globals())}s") | |
print(f"in tuple: {timeit.timeit('in_undefined_tuple(value)', globals=globals())}s") | |
print(f"any set: {timeit.timeit('any_undefined_set(value)', globals=globals())}s") | |
print(f"any tuple: {timeit.timeit('any_undefined_tuple(value)', globals=globals())}s") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment