Compact Python library for math statistics

beliavsky at aol.com beliavsky at aol.com
Fri Apr 9 17:08:41 EDT 2004


python at rcn.com (Raymond Hettinger) wrote in message news:<5d83790c.0404090024.5cefb2ea at posting.google.com>...

<SNIP>

> > (2) The following code crashes when median(x) is computed. Why?
> > 
> > from statistics import mean,median
> > x = [1.0,2.0,3.0,4.0]
> > print mean(x)
> > print median(x)
> 
> Hmm, it works for me.  What does your traceback look like?

The module statistics.py imports a module 'random'. I have my own file
random.py, and it was importing that. My mistake -- sorry.
 
> > (3) The standard deviation is computed as 
> > 
> >     return variance(data, sample) ** 0.5
> > 
> > I think the sqrt function should be used instead -- this may be
> > implemented more efficiently than general exponentiation.
> 
> The timings show otherwise:
> 
> C:\pydev>python timeit.py -r9 -n100000 -s "import math;
> sqrt=math.sqrt" "sqrt(7.0)"
> 100000 loops, best of 9: 1.7 usec per loop
> 
> C:\pydev>python timeit.py -r9 -n100000 -s "7.0 ** 0.5"
> 100000 loops, best of 9: 0.237 usec per loop

For the Compaq and Lahey/Fujitsu Fortran 95 compilers I found that
sqrt(x) and x**0.5 take the same time -- probably the compiler
converts the latter to the former. On one compiler I found that
computing x**0.49 takes about 10 times longer than sqrt(x), indicating
that a sqrt function should be considerably faster than real
exponentiation.

I wonder if for Python, psyco eliminates the speed difference between
sqrt(x) and x**0.5. Otherwise, the speed difference may indicate a
fundamental problem in using a scripting language like Python for
numerical work -- function calls take too much time. Because of that,
sqrt is much slower than real exponentiation, when it should be much
faster.

Overall, the Python code below is about 100 times slower than the
Fortran equivalent. This is a typical ratio I have found for code
involving loops.

from math import sqrt
n = 10000000 + 1
sum_sqrt = 0.0
for i in range(1,n):
    sum_sqrt = sum_sqrt + (float(i))**0.5
print sum_sqrt



More information about the Python-list mailing list