Speed *triples* by changing 'apply(f, args)' to 'f(*args)'

Tim Peters tim.one at comcast.net
Tue Mar 4 20:54:22 EST 2003


[Andrew Dalke]
> There's an annoying feature in python that various things
> generate warnings, which the warning module suppresses.  This
> is more annoying in the performance sense, because it's hard to
> see something which isn't there.
>
> You should be able to get a list of those warnings with the
>   -Wall
> command-line argument to Python.

[Gerrit Holl]
>> Then I changed in sprite.py:311, apply(f, args) to f(*args).
>> Framespeed tripled:

[Andrew]
> Huh.  I wouldn't have thought it would be that fast.  The first
> requires a builin lookup for 'apply', local lookups for 'f' and
> 'args', and two function calls.  The latter only requires the two
> local lookups and a single function call.

There's more than meets the eye:  because the apply() builtin is deprecated
now, the implementation of __builtin__.apply() now calls warnings.warn() (a
Python function) to see whether it should issue a deprecation warning.  So
the former also costs a new hidden call to a long Python function now, which
in turn does many lookups and additional calls (both C and Python level) of
its own.  Luckily, Gerritt already figured out the best fix <wink>.






More information about the Python-list mailing list