Skip to content

Commit c8cc1ea

Browse files
committed
Add isinstance_multiple.
1 parent 67a1c65 commit c8cc1ea

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import timeit
3+
from textwrap import dedent
4+
5+
"""Multiple isinstance checks."""
6+
7+
setup = """\
8+
class A():
9+
pass
10+
11+
data = "asdf"
12+
"""
13+
14+
tests = {
15+
"separate_early": """\
16+
isinstance(data, str) or isinstance(data, float) or isinstance(data, A)
17+
""",
18+
"separate_late": """\
19+
isinstance(data, float) or isinstance(data, A) or isinstance(data, str)
20+
""",
21+
"together_early": """\
22+
isinstance(data, (str, float, A))
23+
""",
24+
"together_late": """\
25+
isinstance(data, (float, A, str))
26+
""",
27+
}
28+
29+
results = {}
30+
for test_name, test_stmt in tests.items():
31+
times = timeit.repeat(
32+
setup=dedent(setup).rstrip(),
33+
stmt=dedent(test_stmt).rstrip(),
34+
repeat=1000,
35+
number=1000,
36+
globals={},
37+
)
38+
avg_time = sum(times) / len(times)
39+
results[test_name] = avg_time
40+
41+
for test_name, result in sorted(results.items(), key=lambda x: x[1]):
42+
print(f"{test_name:>18}: {result:03g}")

0 commit comments

Comments
 (0)