inline_min = min
while expr:
if inline_min(blah):inline_min = min
while expr:
if inline_min(blah):Imports are super imperative operations that first tries to find it in sys.modules, otherwise executes the module and puts the resulting dict in sys.modules. Then it grabs the symbols you asked for and shoves all those symbols into the global dict for the module.
It does have to walk the locals and the globals scopes (there are ONLY exactly two scopes in Python!) to find the function, that's true.
right, this is what I meant.
I am by no means a python expert :) I just use it occasionally.
This “everything is a dict” is also a part of why removing GIL is not as straightforward as it would seem.
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)Using the if statement: Execution time: 0.00648 seconds
Using the min function directly: Execution time: 0.02298 seconds
Using the min function with an intermediate variable: Execution time: 0.02959 seconds
It is consistently around 8% to 15% faster on 3.10.12 and 3.11 for me. On 3.12.5 (latest) I seem to get the same result.
https://www.online-python.com/B6AgKW5zod
(please copy the code to your local to not ddos this site :D)