[Python-ideas] Specifying constants for functions

Guido van Rossum guido at python.org
Tue Oct 27 14:18:56 EDT 2015


IIRC it's an old micro-optimization; this was a common idiom at Zope. But I
think it's way overused -- people believe it works so they do it all the
time, even for code that's not performance sensitive, just because it's
become a habit. (Like "register" in C in the '80s.)

On Tue, Oct 27, 2015 at 10:55 AM, Yury Selivanov <yselivanov.ml at gmail.com>
wrote:

> 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151027/aa9792f4/attachment.html>


More information about the Python-ideas mailing list