I have been looking at the speed of Python with the Numeric module by simulating
some random numbers and computing some statistics. Here is the code. Line
(1) is replaced as shown
in the table.
from RandomArray import random
from Numeric import sum
n = 1000000
m = 10
for i in range(m):
xx = random(n)
print xx[n-1] # (1)
The times shown are in seconds and are the lowest values from several runs.
line (1) Python Fortran
xx[n-1] 1.33 0.52
sum(xx) 1.43 0.54
sum(xx**1) 3.76 0.54
sum(xx**2) 3.71 0.54
sum with explicit loop 11.79 0.54
My conclusions are that
(1) Using the sum function of Numeric is much faster than an explicit loop.
(2) Python with Numeric does not recognize that sum(xx**1) reduces
to sum(xx), and this should be fixed. Obviously one would not write xx**1
explicitly in code, but it is possible that a variable exponent could equal
1.
(3) The range of Fortran/Python speeds here is 2.5 to 7.0, when the Numeric
module is used.
The Fortran code is below. Line (1) is replaced as appropriate. The Python
version is 2.3.3 , the Fortran compiler is Compaq Visual Fortran 6.6c, and
the platform is an Intel Pentium 4 2.80 GHz on Windows XP.
program xran_sum
integer, parameter :: n = 1000000, m = 10
real :: xx(n)
integer :: i
call random_seed()
do i=1,m
call random_number(xx)
print*,xx(n) ! (1)
end do
end program xran_sum |