biais.org

Saturday 10 March 2007

Periodic Table of Visualization Methods

A Periodic Table regrouping many kind of visualization methods.

Friday 9 March 2007

Comparing reduce and classic loop performances

When I talk with other pythoners, then often argue that reduce, map and filter builtins + lambdas are faster than a classic for loop or list comprehensions.

Notes:

  • mip3 is here to verify that you must use standard library functions when it's possible.
  • profilteit is described here
@profileit(0)
def mip0(iterable):
    sum = 0
    for i in iterable:
        sum += i
    print sum
 
@profileit(0)
def mip1(iterable):
    print reduce(lambda x, y: x + y, iterable, 0)
 
import operator
@profileit(0)
def mip2(iterable):
    "Wrap a builtin in reduce will avoid heavy python function calls"
    print reduce(operator.add, iterable, 0)
 
@profileit(0)
def mip3(iterable):
    print sum(iterable)
 
for i in range(4):
    exec "mip%d(xrange(500000))" % i
$ python2.5 tips.py|grep CPU
         1 function calls in 0.555 CPU seconds
         500001 function calls in 2.072 CPU seconds
         1 function calls in 0.666 CPU seconds
         1 function calls in 0.430 CPU seconds

Even mip2 where reduce wrap operator.add (a C builtin) is not the fastest function. Of course, the sum builtin is the fastest way to sum a list ;), but the first, and I think, the most readable version, is a good choice when builtin doesn't exist.