4 ms·
It makes a slight difference on my system, python 3.12.0 In [1]: def test_1(): ...: i = 10_000_000 ...: r = 0 ...: ...: while i
by b3orn 2y ago
It makes a slight difference on my system, python 3.12.0
In [1]: def test_1():
...: i = 10_000_000
...: r = 0
...:
...: while i>0:
...: i = i-1
...: r += min(i, 500)
...:
...: return r
In [2]: def test_2():
...: i = 10_000_000
...: r = 0
...: local_min = min
...:
...: while i>0:
...: i = i-1
...: r += local_min(i, 500)
...:
...: return r
In [3]: %timeit -n 1 -r 10 test_1()
2.14 s ± 58.4 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)
In [4]: %timeit -n 1 -r 10 test_2()
1.94 s ± 47.8 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)