Numerical Python question

Alex Martelli aleaxit at yahoo.com
Sun Oct 12 06:27:24 EDT 2003


David Mertz wrote:
   ...
> But in general, an average is 'sum(highs)/len(highs)'.  The version of
> 'sum()' in Numeric will work a lot faster though... although it makes no
> difference for 10 prices.  If you start worrying about a million prices,
> you might see a significant boost using Numeric.

No kidding: Numeric.sum is an *order of magnitude faster* for this:

[alex at lancelot pop]$ timeit.py -s'import Numeric' 
-s'x=Numeric.array(map(float,range(1000000)))' 'Numeric.sum(x)'
10 loops, best of 3: 2.11e+04 usec per loop

[alex at lancelot pop]$ timeit.py -s'import Numeric' 
-s'x=Numeric.array(map(float,range(1000000)))' 'sum(x)'
10 loops, best of 3: 3.05e+05 usec per loop

the difference between 21 milliseconds with Numeric.sum, and 300 with
the built-in sum, can be *very* significant if you sum millions of floats in
your bottlenecks.

I think the chapter on Numeric in "Python in a Nutshell" is a good intro
(thanks to lots of crucial input from Eric Jones and Paul DuBois!), and
you can read it for free with the usual trick -- get an O'Reilly safari
subscription and use the free first two weeks to read the parts that
interest you, then cancel the subscription so you won't have to pay.


Alex





More information about the Python-list mailing list