For numerical computations the book Python scripting for computational science recommends avoiding explicit loops and use vectorized NumPy expressions instead. The author Hans Petter Langtangen writes that “a speed-up factor of 10 is often gained” (page 427). The same kind of recommendation is also often give for Matlab programs.

With the timeit module short statements or expressions can be measured. The module runs a piece of code a large number of times. With that module we can measure the speed of a classic for loop, a “list comprehension” for loop and a map implementation of the same small list/array computation:

On my 2’500 DKK Intel Atom N450 netbook the results are 13.4, 8.7 and 16.6, while our department CIMBI server reports 4.1, 2.8 and 4.5. The middle one has no function evaluations, while the two others have either a ‘append’ or a ‘lambda’ function evaluation. That might be the reason why the middle one is faster.