The Industry choice
beliavsky at aol.com
beliavsky at aol.com
Fri Dec 31 17:47:41 CET 2004
Bulba wrote:
>OK, so what projects and why would you consider
>Python:
>1. "clearly unsuitable"
Large-scale scientific computing projects, such as numerical weather
prediction, where performance is critical. Python could be used as the
"glue" but not the "guts", where Fortran 95 and C++ are more
appropriate. In my tests, some posted here, there has been a
significant speed differential between Fortran 95 and Python with
Numeric. I don't know if using Numarray instead would change the
results.
Calling an extension written in C, such as Numeric, can improve
performance greatly over using "pure" Python, but the speed of
evaluating simple expressions can still be close to 2 orders of
magnitude slower.
Here is an example that computes
(1) sum(x)
(2) sum(x**2)
(3) sum((x-0.5)**2)
for 1E7 uniformly distributed random numbers.
# Python
from RandomArray import random
from time import time
from Numeric import sum
n = 10000000
half = 0.5
x = random(n)
for iopt in range(1,4):
t1 = time()
if (iopt == 1):
xsum = sum(x)
elif (iopt == 2):
xsum = sum(x**2)
elif (iopt == 3):
xsum = sum((x-half)**2)
t2 = time()
print iopt,xsum,t2-t1
! Fortran 95
program xsum_power
implicit none
integer, parameter :: n = 10000000
real(kind=8) :: x(n),xsum,t1,t2
real , parameter :: half = 0.5
integer :: iopt
iopt = 1
call random_seed()
call random_number(x)
do iopt=1,3
call cpu_time(t1)
select case (iopt)
case (1) ; xsum = sum(x)
case (2) ; xsum = sum(x**2)
case (3) ; xsum = sum((x-half)**2)
end select
call cpu_time(t2)
write (*,"(i4,100f16.6)") iopt,xsum,t2-t1
end do
end program xsum_power
The approximate times taken in seconds, in Python and Fortran, are
task Fortran Python
sum(x) 0.05 0.1
sum(x**2) 0.05 2.4
sum((x-0.5)**2) 0.05 3.2
In the Fortran code, essentially all the time is spent accessing the
array elements. Powers and subtractions in array operations seem to be
free in Fortran but very expensive in Python with Numeric.
Not wanting to be guilty of "premature optimization", I did not try to
make either code more efficient than my first attempt. The Fortran
compiler is Compaq Visual Fortran 6.6c, and the Python used is 2.2.
More information about the Python-list
mailing list