Skip to content

Commit e01e49f

Browse files
committed
Perf demo of zero cost exceptions.
1 parent f0f67fd commit e01e49f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import datetime
2+
from dis import dis
3+
4+
5+
def troubling_method(x: str) -> int:
6+
# print(f"We will take a shot at converting {x}")
7+
8+
try:
9+
return int(x)
10+
except ValueError as ve:
11+
print(f"Oh no, it couldn't be converted because of values: {ve}")
12+
except NotImplementedError:
13+
print("Somehow not implemented?")
14+
except:
15+
print("We have no idea")
16+
17+
# print("There was some kind of error")
18+
return -1
19+
20+
21+
def main():
22+
troubling_method("77")
23+
24+
# print(troubling_method("77"))
25+
# print(troubling_method("901"))
26+
# # print(troubling_method(902))
27+
# print(troubling_method("Nine hundred"))
28+
#
29+
# dis(troubling_method)
30+
31+
times = 10_000_000
32+
t0 = datetime.datetime.now()
33+
34+
for _ in range(times):
35+
troubling_method("77")
36+
37+
t1 = datetime.datetime.now()
38+
dt = t1-t0
39+
print(f"Done in {dt.total_seconds()* 1000:,.2f} ms")
40+
41+
42+
if __name__ == '__main__':
43+
main()

0 commit comments

Comments
 (0)