Long integers, xrange and number theory

Michael Hudson mwh at python.net
Mon Dec 9 06:06:02 EST 2002


"Terry Reedy" <tjreedy at udel.edu> writes:

> Your utility functions (wrapped reduces) would run faster if you
> import operator and use the C-coded functions contained therein
> instead of Python-coded lambdas.  IE, change last line of
> def sum(numlist):
>     """return the sum of a list of numbers"""
>     return reduce(lambda x, y : x+y, numlist, 0)
> to
>     return reduce(operator.add, numlist, 0)
> 

I've said this before, recently, but you will likely go faster with a
for loop.  Consider this script:

import operator, time

def testrl(l):
    T = time.time()
    x = reduce(lambda x,y:x+y, l, 0)
    print time.time() - T

def testro(l):
    T = time.time()
    x = reduce(operator.add, l, 0)
    print time.time() - T

def testf(l):
    T = time.time()
    x = 0
    for y in l:
        x += y
    print time.time() - T

def testf2(l):
    T = time.time()
    x = 0
    for y in l:
        x = x + y 
    print time.time() - T

for i in range(10, 20):
    l = range(2**i)
    print 2**i
    testrl(l)
    testro(l)
    testf(l)
    testf2(l)

the tail of its output on my machine is:

262144
0.816619992256
0.543640017509
0.5033659935
0.495182991028
524288
1.79207909107
1.20976996422
1.1175519228
1.09949707985

so, yes, using operator is faster than using lambda, but avoiding the
function call overhead entirely saves more.

-- 
  ZAPHOD:  OK, so ten out of ten for style, but minus several million
           for good thinking, eh?
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 2



More information about the Python-list mailing list