Currying with instancemethod (was Re: [Python-Dev] accumulator
display syntax)
Alex Martelli
aleaxit at yahoo.com
Fri Oct 17 18:05:24 EDT 2003
On Friday 17 October 2003 11:45 pm, Phillip J. Eby wrote:
...
> At 10:40 PM 10/17/03 +0200, Alex Martelli wrote:
> >Yes, def curry(func, arg): return new.instancemethod(func, arg, object)
...
> >def curry(func, arg):
> > def curried(*args): return func(arg, *args)
> > return curried
...
> It is a big win if the curried function will be used in a
> performance-sensitive way. Instance method objects don't pay for setting
> up an extra frame object, and for the single curried argument, the
> interpreter even shortcuts some of the instancemethod overhead! So, if I
You're right: the instancemethod version has impressively better performance
(should the curried function be used in a bottleneck, of course) -- i.e.,
given a.py:
import new
def curry1(func, arg):
return new.instancemethod(func, arg, object)
def curry2(func, arg):
def curried(*args): return func(args, *args)
return curried
def f(a, b, c): return a, b, c
I've measured:
[alex at lancelot ba]$ timeit.py -c -s'
import a
g = a.curry2(a.f, 23)
' 'g(45, 67)'
100000 loops, best of 3: 2 usec per loop
[alex at lancelot ba]$ timeit.py -c -s'
import a
g = a.curry1(a.f, 23)
' 'g(45, 67)'
1000000 loops, best of 3: 1.09 usec per loop
I sure didn't expect an almost 2:1 ratio, while you did predict it.
Alex
More information about the Python-Dev
mailing list