Speed Comparison Perl Python & C

beliavsky at aol.com beliavsky at aol.com
Tue Mar 2 09:49:31 EST 2004


"Neil Hodgson" <nhodgson at bigpond.net.au> wrote in message news:<lMN0c.83771$Wa.45964 at news-server.bigpond.net.au>...

>    It actually compares the speed of integers large enough to need 64 bits
> of precision where Fortran can use a 64 bit integer and Python uses an
> unbounded integer. The test can be sped up in two ways, first by using
> floating point (which is fixed length in Python) and by using Psyco. On my
> machine:
> 
> Original test: 245 seconds
> Using floats: 148 seconds
> Using psyco with integers: 69 seconds
> Using psyco with floats: 18.4 seconds
> 
>    Therfore gaining a speed up of 13 times. This leads to Fortran likely
> remaining 22 times faster.
> 
> from psyco.classes import *
> import psyco
> 
> def xx():
>     i = 1.0
>     j = 0.0
>     while i < 100000000.0:
>        j = j + i
>        i = i + 1
>     print int(j)
> 
> psyco.profile()
> 
> xx()
> 
>    Neil

Thanks. I am going to learn about Psyco. In this case, I assume that
doing the computations with floating point numbers and finally
converting the result to int gives the same values as the original
integer calculation. In other cases, integer arithmetic will need to
be done with integers to ensure correct results.

Python is supposed to be easy (and in general I agree that it is), but
your solution requires some knowledge of

(1) how integer and floating point calculations are done (which many
novices do not have)
(2) when Psycho can speed things up

and the final result is still much slower than Fortran. For the
Fortran program, the only "trick" is the use of integer*8.



More information about the Python-list mailing list