Compact Python library for math statistics

beliavsky at aol.com beliavsky at aol.com
Sun Apr 11 14:25:55 EDT 2004


Josiah Carlson <jcarlson at uci.edu> wrote in message news:<c59k27$jbi$1 at news.service.uci.edu>...
> > 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
> 
> Yeah...you may want to consider doing some optimizations to the above 
> code.  Using 'xrange' instead of 'range' is significantly faster 
> (especially when your machine can't hold 'n' integers in a Python list 
> in memory), as is the removal of the 'float(i)' cast (which is unnecessary).

My original code, the code with range replaced by xrange, and the code
with the further replacement of "float(i)" with "i" take 22.0, 20.5,
and 14.4 seconds. So it looks like removing unnecessary casts can save
substantial time. Thanks.



More information about the Python-list mailing list