Function application optimization.
Duncan Booth
duncan at NOSPAMrcp.co.uk
Fri Dec 12 05:57:28 EST 2003
Jacek Generowicz <jacek.generowicz at cern.ch> wrote in
news:tyf7k128j09.fsf at pcepsft001.cern.ch:
> Given
>
> fncs = [func1, func2, ..., funcN]
> args = [arg1, arg2, ..., argN]
>
> How should one spell
>
> results = map(lambda f,a: f(a), fncs, args)
>
> in order to get the result most quickly ?
>
Well, the way you wrote it isn't actually bad. The obvious alternative
(using a list comprehension) is slightly slower, but there isn't an awful
lot to choos between any of them, I suspect most of the time goes in the
actual f(a) function call. Any of these methods runs at about 300,000
function calls/second on my slow laptop.
>>> setup = """import itertools
def fn1(a): pass
fns = [fn1] * 100
args = [0] * 100
"""
>>> stmt1 = "result = [ f(a) for (f,a) in itertools.izip(fns, args) ]"
>>> stmt2 = "result = [ f(a) for (f,a) in zip(fns, args) ]"
>>> t = timeit.Timer(stmt1, setup)
>>> t.repeat(3,10000)
[3.5571303056673855, 3.5537404893639177, 3.5594278043718077]
>>> t = timeit.Timer(stmt2, setup)
>>> t.repeat(3,10000)
[3.893281967400867, 3.87834794645687, 3.8829105375124868]
>>> setup = """import itertools
def fn1(a): pass
fns = [fn1] * 1000
args = [0] * 1000
"""
>>> t = timeit.Timer(stmt1, setup)
>>> t.repeat(3,1000)
[3.3503928571927304, 3.3343195853104248, 3.3495254285111287]
>>> t = timeit.Timer(stmt2, setup)
>>> t.repeat(3,1000)
[3.8062683944467608, 3.7946001516952492, 3.7881063096007779]
>>> stmt3 = "results = map(lambda f,a: f(a), fns, args)"
>>> t = timeit.Timer(stmt3, setup)
>>> t.repeat(3,1000)
[3.3275902384241363, 3.3010907810909202, 3.3174872784110789]
The f(a) call is taking about half the time in any of these methods, so you
aren't going to get very much improvement whatever you do to the loop.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list