Skip to content

Instantly share code, notes, and snippets.

@vxgmichel

vxgmichel/reverse.py Secret

Created Apr 20, 2018
Embed
What would you like to do?
Compare two integer reverse functions
In [1]: f1 = lambda n: int(str(n)[::-1])
In [2]: def f2(n, v=0):
...: while n:
...: n, r = divmod(n, 10)
...: v = v*10 + r
...: return v
...:
In [3]: assert f1(123456789) == 987654321
In [4]: assert f2(123456789) == 987654321
In [5]: %timeit f1(123456789)
416 ns ± 1.93 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [6]: %timeit f2(123456789)
1.42 µs ± 9.58 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.