Velocity Reviews > Numeric speed

# Numeric speed

beliavsky@aol.com
Guest
Posts: n/a

 04-30-2004

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

Josiah Carlson
Guest
Posts: n/a

 05-05-2004
You seem to be new at Python, so I'll give you a some information that
you should know before talking about benchmarking Python:

1. Python is slow.

2. Python evaluates the arguments of a function call before calling the
function. That is, when evaluating the following
sum(xx**1)
xx**1 is evaluated before sum is called. Those who program and/or know
mathematics, call this precidence. Since Python does not include an
optimizer, xx**1 is always called, even though it doesn't do anything.

3. Your benchmark looks to really be testing the performance of the
random number generating array function and the sum functions. I
believe that both are implemented in C. Your "benchmark" therefore, is
benchmarking C vs Fortran. I thought such things were out of vogue 10
years ago. Speaking of which, when will people stop comparing Fortran
(whose optimizing compilers have had 30+ years to evolve) against Python
(whose compilers don't optimize in the classic sense).

4. Posting "benchmarks" without posting the entirety of the code used to
produce the results is fundamentally useless. Did you use the unix
'time' command? Did you use the Python 'time' module? Etcetera.

- Josiah

P.S. Python being slower that Fortran or (insert your favorite language
here) wouldn't surprise me. While Python has been getting faster over
the years, the design considerations are for usability first, speed later.