Ways to make a free variable local to a function?

Yubin Ruan ablacktshirt at gmail.com
Sun Apr 15 09:58:03 EDT 2018


On 2018-04-15 13:31, Kirill Balunov wrote:
> 
> 
> 2018-04-15 10:58 GMT+03:00 Yubin Ruan <ablacktshirt at gmail.com>:
> 
>     [this is a bit late...]
> 
>     Did you really have any benchmark for it? I know what you are doing but it
>     seems to be a pre-mature optimization. If this really is the case, then we
>     can
>     optimize the Python interpreter.
> 
> 
> I don't know if you intentionally send this message privately to me and not to
> the entire python-list. If it was a mistake, post it there and I will answer
> there too.

Sorry I mistakenly hit the 'Reply' button instead of 'Group-reply'.
 
> Yes I've measured:
> 
> 
>     def func(numb):
>         res = []
>         for i in range(numb):
>             res.append(int(i) + float(i))
>         return res
> 
>     def func_local(numb, _int = int, _float = float):
>         res = []
>         for i in range(numb):
>             res.append(_int(i) + _float(i))
>         return res

Wouldn't this kind of things better be (implicitly) used in the Python
interpreter instead of in this kind of hand-optimized code? That looks
strange.

Nevertheless, if need be, I would opt for that 'static-declared' approach you
described earlier, which give semantic hint on the optimization used.

Thanks,
Yubin
 
>     %timeit func(100000)
> 
>     85.8 ms ± 2.47 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
> 
> 
>     %timeit func_local(100000)
> 
>     72.2 ms ± 892 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
> 
> So in the tight loop it is 16% faster.



More information about the Python-list mailing list