Skip to content

Instantly share code, notes, and snippets.

@and-semakin
Last active November 12, 2021 17:02
Show Gist options
  • Save and-semakin/fedf3788e0738e4d5718e1e67ff83276 to your computer and use it in GitHub Desktop.
Save and-semakin/fedf3788e0738e4d5718e1e67ff83276 to your computer and use it in GitHub Desktop.
Python 3.10 vs. 3.11 comparison
❯ ipython
Python 3.10.0 (default, Oct 25 2021, 23:33:57) [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: def no_check(a, b):
...: return a / b
...:
...:
...: def try_check(a, b):
...: try:
...: return a / b
...: except ZeroDivisionError:
...: return None
...:
...:
...: def if_check(a, b):
...: if b != 0:
...: return a / b
...: else:
...: return None
...:
In [2]: %timeit no_check(1, 1)
115 ns ± 0.693 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [3]: %timeit no_check(1, 1)
114 ns ± 0.103 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [4]: %timeit try_check(1, 1)
121 ns ± 0.42 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [5]: %timeit try_check(1, 1)
120 ns ± 0.502 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [6]: %timeit try_check(1, 0)
371 ns ± 1.07 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [7]: %timeit try_check(1, 0)
370 ns ± 1.35 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [8]: %timeit if_check(1, 1)
130 ns ± 0.152 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [9]: %timeit if_check(1, 1)
130 ns ± 0.144 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [10]: %timeit if_check(1, 0)
109 ns ± 0.0714 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [11]: %timeit if_check(1, 0)
109 ns ± 0.0759 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [12]:
❯ ipython
Python 3.11.0a2 (main, Nov 12 2021, 20:55:05) [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: def no_check(a, b):
...: return a / b
...:
...:
...: def try_check(a, b):
...: try:
...: return a / b
...: except ZeroDivisionError:
...: return None
...:
...:
...: def if_check(a, b):
...: if b != 0:
...: return a / b
...: else:
...: return None
...:
In [2]: %timeit no_check(1, 1)
76.9 ns ± 0.0742 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [3]: %timeit no_check(1, 1)
76.9 ns ± 0.0958 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [4]: %timeit try_check(1, 1)
79.4 ns ± 0.0851 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [5]: %timeit try_check(1, 1)
78.2 ns ± 0.212 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [6]: %timeit try_check(1, 0)
360 ns ± 0.688 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [7]: %timeit try_check(1, 0)
361 ns ± 0.959 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [8]: %timeit if_check(1, 1)
94.9 ns ± 1.07 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [9]: %timeit if_check(1, 1)
94 ns ± 0.754 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [10]: %timeit if_check(1, 0)
75.8 ns ± 0.153 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [11]: %timeit if_check(1, 0)
76.4 ns ± 0.0665 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [12]:
def no_check(a, b):
return a / b
def try_check(a, b):
try:
return a / b
except ZeroDivisionError:
return None
def if_check(a, b):
if b != 0:
return a / b
else:
return None
%timeit no_check(1, 1)
%timeit try_check(1, 1)
%timeit try_check(1, 0)
%timeit if_check(1, 1)
%timeit if_check(1, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment