[Python-ideas] Specifying constants for functions
Yury Selivanov
yselivanov.ml at gmail.com
Tue Oct 27 13:55:30 EDT 2015
Serhiy,
On 2015-10-27 1:45 PM, Serhiy Storchaka wrote:
> There is known trick to optimize a function:
>
> def foo(x, y=0, len=len, pack=struct.pack, maxsize=1<<BPF):
> ...
>
> It has a side effect: change function's signature. Would be nice to
> have a way to set function's local variables at creation time without
> affecting a signature.
I see this a lot in all kinds of code. In my experience it doesn't
actually speed things up in a measurable way.
Is the below code really much slower?
def foo(x, y=0):
pack=struct.pack
maxsize=1<<BPF
#CODE
If the #CODE is a tight long-running loop - then no, because the loop
will probably run much longer than an extra attribute lookup + one extra
bit shift on each "foo()" call. And if there is no tight loop - then
you won't probably notice those optimizations anyways.
I think that adding a "const" statement deserves some discussion, but
not from the standpoint of micro-optimizations.
Thanks,
Yury
More information about the Python-ideas
mailing list