Re: Performance comparison Q

Brian Kelley <[email protected]> Mon, 9 May 2005 15:11:51 -0400
Newsgroups gmane.comp.db.metakit
Message-ID <[email protected]>
You can always use reduce which operators on a function of two parameters

def f(a,b):
    if b%7>3: return a+b
    return a

data = range(10)
print reduce(f, data)

or you could use a prefilter to generate a temporary list:

reduce(operator.add, [x for x in data if x%7>3])

in python 2.4 you can use an expression generator for the list
comprehension above:

reduce(operator.add, (x for x in data if x%7>3) )

which will not make a temporary list.  Generators are essentially
co-routines that act like iterators.  Co-routines can yield execution
until the next element in the iterator is requested.  Using sum
instead of reduce(operator.add... will still be faster)

see 
http://www.python.org/peps/pep-0289.html 
for language specifications.

Brian
_____________________________________________
Metakit mailing list  -  [email protected]
http://www.equi4.com/mailman/listinfo/metakit