Compact Python library for math statistics
jcarlson at uci.edu
Sat Apr 10 22:02:01 CEST 2004
> 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).
As for Python being slow compared to Fortran, of course it is going to
be slow in comparison. Fortran is compiled to assembly, and has fairly
decent (if not amazing) optimizers. Python is bytecode compiled,
interpreted, and lacks an even remotely equivalent optimizer.
More information about the Python-list