The secrets of operator.add

Paul Prescod paulp at ActiveState.com
Fri Dec 8 12:21:56 EST 2000


Jacek Generowicz wrote:
> 

>    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>     49999    0.840    0.000    0.840    0.000 <string>:1(<lambda>)
>         1    0.720    0.720    1.560    1.560 <string>:1(?)
>         0    0.000             0.000          profile:0(profiler)
>         1    0.040    0.040    1.600    1.600 profile:0(reduce(lambda x,y:x+y, range(N)))
> 
> ...
>    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>         1    0.060    0.060    0.060    0.060 <string>:1(?)
>         0    0.000             0.000          profile:0(profiler)
>         1    0.000    0.000    0.060    0.060 profile:0(reduce(add, range(N)))
> 
> It looks as if multiple calls to operator.add are avoided somehow
> . . . or am I failing to understand the profiler's output ?

Operator.add is a builtin, not a Python function so calling it is very
quick and it doesn't really show up in the profiler.

> Is there a way of getting similar advantages in
> 
> run ('map(lambda x:x+1, range(N))')  ?

How about:

run('[x+1 for x in range(N)]')

> Is there a Python way of currying add?

lambda is the way to do currying. It may not be as efficient as you like
but that's how it works...

 Paul Prescod




More information about the Python-list mailing list