Speed Comparison Perl Python & C

Josiah Carlson jcarlson at nospam.uci.edu
Mon Mar 1 22:32:55 CET 2004

> i = 1
> j = 0
> while i < 100000000:
>    j = j + i
>    i = i + 1
> print j
> Python   93.4
> Fortran   0.28

Not quite fair.  The Python version does long-integer 
(infinite-precision) math when i is greater than 65534 on a 32 bit 
processor.  You are really counting is the time to do 100,065,535 
integer additions and 99934465 long-integer additions.

Furthermore, you allow fortran to optimize the loop, but you don't 
optimize the python loop yourself, using those portions built-in that 
are known to be fast.

print sum(xrange(100000000))
takes half-as long as the while loop on my machine, yet does the exact 
same calculations.

Your Fortran definition includs a message saying "use 64-bit integers here":
integer*8 :: i,j

You are comparing apples to oranges on multiple levels.  Don't.

  - Josiah

More information about the Python-list mailing list