SlideShare a Scribd company logo
A Peek on Numerical Programming in Perl and
Python
E. Christopher Dyken∗
Abstract
In this note we peek at the capabilities for numerical programming in the two
high-level scripting languages Perl and Python, both having numerical libraries
providing increased efficiency for iterating over large arrays of data. We do a
superficial investigation on the efficiency of both languages with and without using
numerical libraries.
Using high-level languages can quite often increase the speed substantially for software
development. High-level scripting languages makes rapid prototyping of new ideas and
concepts possible with a minimal amount of effort. However, one crux of numerical
software is efficient traversal of large amounts of data. High-level languages per se has
a deficiency in the sense that such operations are notoriously slow. To overcome this,
both Perl and Python has add-on libraries providing special data types that can hold
large chunks of data efficient, in regard to both memory usage as well as access speed.
Given one can formulate one’s algorithm as element-by-element operations over
n-dimensional arrays, both Perl and Python provide functionality with performance
comparable to compiled C code. Numerical Python[1] (NumPy) provides fast multi-
dimensional capabilities to Python. A new implementation, numarray[2], is available
as well. Perl has its own counterpart to NumPy, the Perl Data Language[3] (PDL).
PDL brings number-crunching capabilities to Perl as well as an interactive shell and
other goodies. To shed some light on the numerical capabilities of high-level scripting
languages, we have implemented the trapezoidal quadrature rule in Python and Perl,
both with and without add-on libraries, as well as in standard C for reference.
We used a formulation of the quadrature rule which takes advantage of the type of
element-by-element operations over arrays that are optimized by the add-on libraries,
Z b
a
f(x) dx = h
X
i=0...N
f(a + h · i) −
f(a) + f(b)
2
!
,
where h = (b − a)/(N − 1) and N is the number of samples. The idea is to make each
implementation have a code path as similar to each other as possible.
We used three different integrands f1, f2 and f3, defined as
∗Centre of Mathematics for Applications, University of Oslo
1
f1(x) = x,
f2(x) = x2
and
f3(x) = cos(x2
) sin(x),
where f1 and f2 are simple polynomials, while f3 is a bit more complex involving
some trigonometric functions.
From this we made the following implementations:
A Standard C implementation was used for reference, compiled with GNU gcc
version 3.2.2 using no optimizations whatsoever. The source code can be found
in Appendix A.1.
A Optimized C implementation, identical to the standard C version, but com-
piled with -O3 optimization.
A Standard Python implementation was run using Python version 2.3.3. The
source code can be found in Appendix A.2.
A Python with NumPy implementation was run using Python version 2.3.3 and
NumPy version 23.1. The source code can be found in Appendix A.3.
A Python with numarray implementation was run using Python version 2.3.3
and numarray version 0.9. The source code can be found in Appendix A.4.
A Standard Perl implementation was run using Perl version 5.8.0. The source
code can be found in Appendix A.5.
A Perl with PDL implementation was run using Perl version 5.8.0 and PDL
version 2.4.1. The source code can be found in Appendix A.6.
In order to make the tests as fair as possible, the *nix system call gettimeofday
was used since it is available in all languages used in this test. This system call returns
the number of seconds and microseconds of wall-clock time since the epoch. It is ques-
tionable to use the wall-clock time and not the processor time spent. However, since
all tests were run on the same system under the same levels of system load (virtually
none) several times, it should give a fair indication.
The experiment was carried out by performing each test 100 times for each com-
bination of sampling density, programming language and integrand on a Intel Pentium
4 running at 3.20 GHz with 2 GB of RAM. The performance of each program is plot-
ted in Figures 1, 2 and 3 where average execution time is plotted versus number of
samples.
From the results, we see that plain vanilla Python and Perl doesn’t present a per-
formance comparable to C. This comes as no surprise. However, by using the exten-
sions, the picture changes. Both numarray and PDL exhibits performance relatively
close to C.
NumPy shows a worse performance on the simplest function, but this gap is re-
duced when the integrand becomes more complex. Test functions 1 and 2 is composed
2
of only simple floating point arithmetic, and thus most time is spent in the surround-
ing control structures. However, test function 3 uses cos and sin, which is more
costly and dominates the execution time, which in turn results in a reduction of the gap
between the various implementations.
Thus, from this, we conclude that, given your problem can be formulated mostly
as element-by-element operations on arrays, Python or Perl with add-on libraries could
be a viable alternative to the more traditional approach of C or C++, an alternative that
is definitely worth investigating.
References
[1] The Numerical Python home page,
http://www.numpy.org/
[2] The numarray home page,
http://www.stsci.edu/resources/software_hardware/numarray
[3] The Perl Data Language home page,
http://pdl.perl.org/
3
0
0.02
0.04
0.06
0.08
0.1
0.12
0.14
0 10 20 30 40 50 60 70 80 90 100
Average
time
(sec)
Thousands of samples
Test function 1
C
Python
Perl
0
0.001
0.002
0.003
0.004
0.005
0.006
0.007
0.008
0.009
0 10 20 30 40 50 60 70 80 90 100
Average
time
(sec)
Thousands of samples
Test function 1
C
C -O3
NumPy
numarray
PDL
Figure 1: Numerical integration of f(x) = x.
4
0
0.02
0.04
0.06
0.08
0.1
0.12
0.14
0.16
0 10 20 30 40 50 60 70 80 90 100
Average
time
(sec)
Thousands of samples
Test function 2
C
Python
Perl
0
0.002
0.004
0.006
0.008
0.01
0.012
0 10 20 30 40 50 60 70 80 90 100
Average
time
(sec)
Thousands of samples
Test function 2
C
C -O3
NumPy
numarray
PDL
Figure 2: Numerical integration of f(x) = x2
.
5
0
0.05
0.1
0.15
0.2
0.25
0 10 20 30 40 50 60 70 80 90 100
Average
time
(sec)
Thousands of samples
Test function 3
C
Python
Perl
0
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
0 10 20 30 40 50 60 70 80 90 100
Average
time
(sec)
Thousands of samples
Test function 3
C
C -O3
NumPy
numarray
PDL
Figure 3: Numerical integration of f(x) = cos(x2
) sin(x).
6
A Source code
A.1 C implementation
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
double integrate(double a, double b, double (*f)(double), int N) {
double h = (b-a)/((double)N-1.0);
double sum = 0.0;
int i;
for(i=0; i<N; i++)
sum += f(a+h*(double)i);
return h*(sum - 0.5*f(a) - 0.5*f(b));
}
double f1(double t) { return t; }
double f2(double t) { return t*t; }
double f3(double t) { return cos(t*t)*sin(t); }
int main(int argc, char **argv) {
struct timeval begin, end;
double (*f)(double t), res, a=0.0, b=1.0;
int N, M, i;
if(argc != 3) return -1;
switch(atoi(argv[1])) {
case 0: f = f1; break;
case 1: f = f2; break;
case 2: f = f3; break;
}
M = atoi(argv[2]);
for(N=1000; N<100000; N+=1000) {
gettimeofday(&begin, NULL);
for(i=0; i<M; i++)
res = integrate(0,1,f,N);
gettimeofday(&end, NULL);
printf("%f %fn", res,
((end.tv_sec - begin.tv_sec) +
1.0E-6F*(end.tv_usec - begin.tv_usec))/(double)M);
}
}
7
A.2 Python implementation
import sys;
from math import *;
def integrate(a,b,f,n):
h = (b-a)/(n-1.0);
sum = 0.0;
for i in range(n):
sum += f(a+(i)*h);
return h*(sum - 0.5*f(a) -0.5*f(b));
if __name__ == ’__main__’:
from time import time;
def f1(x): return x;
def f2(x): return x*x;
def f3(x): return cos(x*x)*sin(x);
if len(sys.argv) == 3:
if int(sys.argv[1]) == 0:
f = f1
elif int(sys.argv[1]) == 1:
f = f2
else:
f = f3
a = 0
b = 1
M = int(sys.argv[2])
for N in range(1000, 100000, 1000):
begin = time()
for i in range(M):
res = integrate(a,b,f,N)
avg = (time()-begin)/M
print "%f %f" % (res, avg)
8
A.3 NumPy implementation
import sys;
from Numeric import *;
def integrate(a,b,f,n):
h = (b-a)/(n-1.0);
vals = f(h*arange(n));
return h*(sum(vals) - 0.5*vals[0] - 0.5*vals[len(vals)-1]);
if __name__ == ’__main__’:
from time import time;
def f1(x): return x;
def f2(x): return x*x;
def f3(x): return cos(x*x)*sin(x);
if len(sys.argv) == 3:
if int(sys.argv[1]) == 0:
f = f1
elif int(sys.argv[1]) == 1:
f = f2
else:
f = f3
a = 0
b = 1
M = int(sys.argv[2]);
for N in range(1000, 100000, 1000):
begin = time();
for i in range(M):
res = integrate(a,b,f,N);
avg = (time()-begin)/M;
print "%f %f" % (res, avg)
9
A.4 Numarray implementation
import sys;
from numarray import *;
def integrate(a,b,f,n):
h = (b-a)/(n-1.0);
vals = f(h*arange(n));
return h*(sum(vals) - 0.5*vals[0] - 0.5*vals[len(vals)-1]);
if __name__ == ’__main__’:
from time import time;
def f1(x): return x;
def f2(x): return x*x;
def f3(x): return cos(x*x)*sin(x);
if len(sys.argv) == 3:
if int(sys.argv[1]) == 0:
f = f1
elif int(sys.argv[1]) == 1:
f = f2
else:
f = f3
a = 0
b = 1
M = int(sys.argv[2]);
for N in range(1000, 100000, 1000):
begin = time()
for i in range(M):
res = integrate(a,b,f,N)
avg = (time()-begin)/M
print "%f %f" % (res, avg)
10
A.5 Perl implementation
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes qw(gettimeofday tv_interval);
sub integrate {
my ($a, $b, $f_ref, $n) = @_;
my $h = ($b-$a)/($n-1.0);
my $sum = 0;
for my $i (0..$n-1) {
$sum += $f_ref->($a + $h*$i);
}
return $h*( $sum - 0.5*$f_ref->($a) -0.5*$f_ref->($b));
}
die unless @ARGV == 2;
my @F = ( sub {shift; },
sub {my $x=shift; $x*$x; },
sub {my $x=shift; cos($x*$x)*sin($x)} );
my $f = $F[shift];
my $M = shift;
my ($a, $b) = (0.0, 1.0);
my ($res, $begin, $end, $avg);
for (my $N=1000; $N<100000; $N+=1000) {
$begin = [gettimeofday()];
for my $i (1..$M) {
$res = integrate($a,$b,$f,$N);
}
$avg = tv_interval($begin)/$M;
print "$res $avgn";
}
11
A.6 PDL implementation
#!/usr/bin/perl
use strict;
use warnings;
use PDL;
use Time::HiRes qw(gettimeofday tv_interval);
sub integrate {
my ($a, $b, $f_ref, $n) = @_;
my $h = ($b-$a)/($n-1.0);
my $vals = $f_ref->($h*xvals($n));
return $h*(sum($vals) - 0.5*$vals->index(0) - 0.5*$vals->index($n-1));
}
die unless @ARGV == 2;
my @F = ( sub {shift; },
sub {my $x=shift; $x*$x; },
sub {my $x=shift; cos($x*$x)*sin($x)} );
my $f = $F[shift];
my $M = shift;
my ($a, $b) = (0.0, 1.0);
my ($res, $begin, $avg);
for(my $N=1000; $N<100000; $N+=1000) {
$begin = [gettimeofday()];
for my $i (1..$M) {
$res = integrate($a,$b,$f,$N);
}
$avg = tv_interval($begin)/$M;
print "$res $avgn";
}
12

More Related Content

What's hot (20)

PDF
Yampa AFRP Introduction
ChengHui Weng
 
PPTX
Using Parallel Computing Platform - NHDNUG
North Houston .NET Users Group
 
PDF
LeFlowを調べてみました
Mr. Vengineer
 
PDF
A Simple Communication System Design Lab #4 with MATLAB Simulink
Jaewook. Kang
 
PPTX
190111 tf2 preview_jwkang_pub
Jaewook. Kang
 
PDF
mpi4py.pdf
A Jorge Garcia
 
PDF
Introduction to TensorFlow 2.0
Databricks
 
PDF
Brief Introduction to Cython
Aleksandar Jelenak
 
PDF
Phil Bartie QGIS PLPython
Ross McDonald
 
PPTX
Tail call optimization (TCO) - Lecture
Jesse Frankley
 
PPS
Aae oop xp_06
Niit Care
 
PDF
Concurrency with Go
Frank Müller
 
PDF
Use of an Oscilloscope - maXbox Starter33
Max Kleiner
 
PDF
A closure ekon16
Max Kleiner
 
PDF
What’s eating python performance
Piotr Przymus
 
PDF
Garbage Collection
Eelco Visser
 
PPTX
Introduction to TensorFlow 2 and Keras
Oswald Campesato
 
PDF
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
Edge AI and Vision Alliance
 
PDF
High Performance Python - Marc Garcia
Marc Garcia
 
PPTX
Working with tf.data (TF 2)
Oswald Campesato
 
Yampa AFRP Introduction
ChengHui Weng
 
Using Parallel Computing Platform - NHDNUG
North Houston .NET Users Group
 
LeFlowを調べてみました
Mr. Vengineer
 
A Simple Communication System Design Lab #4 with MATLAB Simulink
Jaewook. Kang
 
190111 tf2 preview_jwkang_pub
Jaewook. Kang
 
mpi4py.pdf
A Jorge Garcia
 
Introduction to TensorFlow 2.0
Databricks
 
Brief Introduction to Cython
Aleksandar Jelenak
 
Phil Bartie QGIS PLPython
Ross McDonald
 
Tail call optimization (TCO) - Lecture
Jesse Frankley
 
Aae oop xp_06
Niit Care
 
Concurrency with Go
Frank Müller
 
Use of an Oscilloscope - maXbox Starter33
Max Kleiner
 
A closure ekon16
Max Kleiner
 
What’s eating python performance
Piotr Przymus
 
Garbage Collection
Eelco Visser
 
Introduction to TensorFlow 2 and Keras
Oswald Campesato
 
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
Edge AI and Vision Alliance
 
High Performance Python - Marc Garcia
Marc Garcia
 
Working with tf.data (TF 2)
Oswald Campesato
 

Similar to A peek on numerical programming in perl and python e christopher dyken 2005 (20)

PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
PPTX
C Programming Homework Help
Programming Homework Help
 
PDF
Python高级编程(二)
Qiangning Hong
 
PPTX
Introduction to data structures and complexity.pptx
PJS KUMAR
 
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
PPTX
cp05.pptx
RehmanRasheed3
 
PDF
EKON 25 Python4Delphi_mX4
Max Kleiner
 
PPTX
Unit i basic concepts of algorithms
sangeetha s
 
PDF
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
PPTX
CPP Homework Help
C++ Homework Help
 
PDF
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
South Tyrol Free Software Conference
 
PDF
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Marina Kolpakova
 
PPT
Code Tuning
guest4df97e3d
 
PDF
C optimization notes
Fyaz Ghaffar
 
PPT
C++ Language
Syed Zaid Irshad
 
PDF
Mpi in-python
A Jorge Garcia
 
PPTX
Introduction to datastructure and algorithm
Pratik Mota
 
PDF
Subtle Asynchrony by Jeff Hammond
Patrick Diehl
 
PDF
What's new in Python 3.11
Henry Schreiner
 
PDF
Tackling repetitive tasks with serial or parallel programming in R
Lun-Hsien Chang
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
C Programming Homework Help
Programming Homework Help
 
Python高级编程(二)
Qiangning Hong
 
Introduction to data structures and complexity.pptx
PJS KUMAR
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
cp05.pptx
RehmanRasheed3
 
EKON 25 Python4Delphi_mX4
Max Kleiner
 
Unit i basic concepts of algorithms
sangeetha s
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
CPP Homework Help
C++ Homework Help
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
South Tyrol Free Software Conference
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Marina Kolpakova
 
Code Tuning
guest4df97e3d
 
C optimization notes
Fyaz Ghaffar
 
C++ Language
Syed Zaid Irshad
 
Mpi in-python
A Jorge Garcia
 
Introduction to datastructure and algorithm
Pratik Mota
 
Subtle Asynchrony by Jeff Hammond
Patrick Diehl
 
What's new in Python 3.11
Henry Schreiner
 
Tackling repetitive tasks with serial or parallel programming in R
Lun-Hsien Chang
 
Ad

Recently uploaded (20)

PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Ad

A peek on numerical programming in perl and python e christopher dyken 2005

  • 1. A Peek on Numerical Programming in Perl and Python E. Christopher Dyken∗ Abstract In this note we peek at the capabilities for numerical programming in the two high-level scripting languages Perl and Python, both having numerical libraries providing increased efficiency for iterating over large arrays of data. We do a superficial investigation on the efficiency of both languages with and without using numerical libraries. Using high-level languages can quite often increase the speed substantially for software development. High-level scripting languages makes rapid prototyping of new ideas and concepts possible with a minimal amount of effort. However, one crux of numerical software is efficient traversal of large amounts of data. High-level languages per se has a deficiency in the sense that such operations are notoriously slow. To overcome this, both Perl and Python has add-on libraries providing special data types that can hold large chunks of data efficient, in regard to both memory usage as well as access speed. Given one can formulate one’s algorithm as element-by-element operations over n-dimensional arrays, both Perl and Python provide functionality with performance comparable to compiled C code. Numerical Python[1] (NumPy) provides fast multi- dimensional capabilities to Python. A new implementation, numarray[2], is available as well. Perl has its own counterpart to NumPy, the Perl Data Language[3] (PDL). PDL brings number-crunching capabilities to Perl as well as an interactive shell and other goodies. To shed some light on the numerical capabilities of high-level scripting languages, we have implemented the trapezoidal quadrature rule in Python and Perl, both with and without add-on libraries, as well as in standard C for reference. We used a formulation of the quadrature rule which takes advantage of the type of element-by-element operations over arrays that are optimized by the add-on libraries, Z b a f(x) dx = h X i=0...N f(a + h · i) − f(a) + f(b) 2 ! , where h = (b − a)/(N − 1) and N is the number of samples. The idea is to make each implementation have a code path as similar to each other as possible. We used three different integrands f1, f2 and f3, defined as ∗Centre of Mathematics for Applications, University of Oslo 1
  • 2. f1(x) = x, f2(x) = x2 and f3(x) = cos(x2 ) sin(x), where f1 and f2 are simple polynomials, while f3 is a bit more complex involving some trigonometric functions. From this we made the following implementations: A Standard C implementation was used for reference, compiled with GNU gcc version 3.2.2 using no optimizations whatsoever. The source code can be found in Appendix A.1. A Optimized C implementation, identical to the standard C version, but com- piled with -O3 optimization. A Standard Python implementation was run using Python version 2.3.3. The source code can be found in Appendix A.2. A Python with NumPy implementation was run using Python version 2.3.3 and NumPy version 23.1. The source code can be found in Appendix A.3. A Python with numarray implementation was run using Python version 2.3.3 and numarray version 0.9. The source code can be found in Appendix A.4. A Standard Perl implementation was run using Perl version 5.8.0. The source code can be found in Appendix A.5. A Perl with PDL implementation was run using Perl version 5.8.0 and PDL version 2.4.1. The source code can be found in Appendix A.6. In order to make the tests as fair as possible, the *nix system call gettimeofday was used since it is available in all languages used in this test. This system call returns the number of seconds and microseconds of wall-clock time since the epoch. It is ques- tionable to use the wall-clock time and not the processor time spent. However, since all tests were run on the same system under the same levels of system load (virtually none) several times, it should give a fair indication. The experiment was carried out by performing each test 100 times for each com- bination of sampling density, programming language and integrand on a Intel Pentium 4 running at 3.20 GHz with 2 GB of RAM. The performance of each program is plot- ted in Figures 1, 2 and 3 where average execution time is plotted versus number of samples. From the results, we see that plain vanilla Python and Perl doesn’t present a per- formance comparable to C. This comes as no surprise. However, by using the exten- sions, the picture changes. Both numarray and PDL exhibits performance relatively close to C. NumPy shows a worse performance on the simplest function, but this gap is re- duced when the integrand becomes more complex. Test functions 1 and 2 is composed 2
  • 3. of only simple floating point arithmetic, and thus most time is spent in the surround- ing control structures. However, test function 3 uses cos and sin, which is more costly and dominates the execution time, which in turn results in a reduction of the gap between the various implementations. Thus, from this, we conclude that, given your problem can be formulated mostly as element-by-element operations on arrays, Python or Perl with add-on libraries could be a viable alternative to the more traditional approach of C or C++, an alternative that is definitely worth investigating. References [1] The Numerical Python home page, http://www.numpy.org/ [2] The numarray home page, http://www.stsci.edu/resources/software_hardware/numarray [3] The Perl Data Language home page, http://pdl.perl.org/ 3
  • 4. 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0 10 20 30 40 50 60 70 80 90 100 Average time (sec) Thousands of samples Test function 1 C Python Perl 0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0 10 20 30 40 50 60 70 80 90 100 Average time (sec) Thousands of samples Test function 1 C C -O3 NumPy numarray PDL Figure 1: Numerical integration of f(x) = x. 4
  • 5. 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0 10 20 30 40 50 60 70 80 90 100 Average time (sec) Thousands of samples Test function 2 C Python Perl 0 0.002 0.004 0.006 0.008 0.01 0.012 0 10 20 30 40 50 60 70 80 90 100 Average time (sec) Thousands of samples Test function 2 C C -O3 NumPy numarray PDL Figure 2: Numerical integration of f(x) = x2 . 5
  • 6. 0 0.05 0.1 0.15 0.2 0.25 0 10 20 30 40 50 60 70 80 90 100 Average time (sec) Thousands of samples Test function 3 C Python Perl 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0 10 20 30 40 50 60 70 80 90 100 Average time (sec) Thousands of samples Test function 3 C C -O3 NumPy numarray PDL Figure 3: Numerical integration of f(x) = cos(x2 ) sin(x). 6
  • 7. A Source code A.1 C implementation #include <stdio.h> #include <math.h> #include <sys/time.h> double integrate(double a, double b, double (*f)(double), int N) { double h = (b-a)/((double)N-1.0); double sum = 0.0; int i; for(i=0; i<N; i++) sum += f(a+h*(double)i); return h*(sum - 0.5*f(a) - 0.5*f(b)); } double f1(double t) { return t; } double f2(double t) { return t*t; } double f3(double t) { return cos(t*t)*sin(t); } int main(int argc, char **argv) { struct timeval begin, end; double (*f)(double t), res, a=0.0, b=1.0; int N, M, i; if(argc != 3) return -1; switch(atoi(argv[1])) { case 0: f = f1; break; case 1: f = f2; break; case 2: f = f3; break; } M = atoi(argv[2]); for(N=1000; N<100000; N+=1000) { gettimeofday(&begin, NULL); for(i=0; i<M; i++) res = integrate(0,1,f,N); gettimeofday(&end, NULL); printf("%f %fn", res, ((end.tv_sec - begin.tv_sec) + 1.0E-6F*(end.tv_usec - begin.tv_usec))/(double)M); } } 7
  • 8. A.2 Python implementation import sys; from math import *; def integrate(a,b,f,n): h = (b-a)/(n-1.0); sum = 0.0; for i in range(n): sum += f(a+(i)*h); return h*(sum - 0.5*f(a) -0.5*f(b)); if __name__ == ’__main__’: from time import time; def f1(x): return x; def f2(x): return x*x; def f3(x): return cos(x*x)*sin(x); if len(sys.argv) == 3: if int(sys.argv[1]) == 0: f = f1 elif int(sys.argv[1]) == 1: f = f2 else: f = f3 a = 0 b = 1 M = int(sys.argv[2]) for N in range(1000, 100000, 1000): begin = time() for i in range(M): res = integrate(a,b,f,N) avg = (time()-begin)/M print "%f %f" % (res, avg) 8
  • 9. A.3 NumPy implementation import sys; from Numeric import *; def integrate(a,b,f,n): h = (b-a)/(n-1.0); vals = f(h*arange(n)); return h*(sum(vals) - 0.5*vals[0] - 0.5*vals[len(vals)-1]); if __name__ == ’__main__’: from time import time; def f1(x): return x; def f2(x): return x*x; def f3(x): return cos(x*x)*sin(x); if len(sys.argv) == 3: if int(sys.argv[1]) == 0: f = f1 elif int(sys.argv[1]) == 1: f = f2 else: f = f3 a = 0 b = 1 M = int(sys.argv[2]); for N in range(1000, 100000, 1000): begin = time(); for i in range(M): res = integrate(a,b,f,N); avg = (time()-begin)/M; print "%f %f" % (res, avg) 9
  • 10. A.4 Numarray implementation import sys; from numarray import *; def integrate(a,b,f,n): h = (b-a)/(n-1.0); vals = f(h*arange(n)); return h*(sum(vals) - 0.5*vals[0] - 0.5*vals[len(vals)-1]); if __name__ == ’__main__’: from time import time; def f1(x): return x; def f2(x): return x*x; def f3(x): return cos(x*x)*sin(x); if len(sys.argv) == 3: if int(sys.argv[1]) == 0: f = f1 elif int(sys.argv[1]) == 1: f = f2 else: f = f3 a = 0 b = 1 M = int(sys.argv[2]); for N in range(1000, 100000, 1000): begin = time() for i in range(M): res = integrate(a,b,f,N) avg = (time()-begin)/M print "%f %f" % (res, avg) 10
  • 11. A.5 Perl implementation #!/usr/bin/perl use strict; use warnings; use Time::HiRes qw(gettimeofday tv_interval); sub integrate { my ($a, $b, $f_ref, $n) = @_; my $h = ($b-$a)/($n-1.0); my $sum = 0; for my $i (0..$n-1) { $sum += $f_ref->($a + $h*$i); } return $h*( $sum - 0.5*$f_ref->($a) -0.5*$f_ref->($b)); } die unless @ARGV == 2; my @F = ( sub {shift; }, sub {my $x=shift; $x*$x; }, sub {my $x=shift; cos($x*$x)*sin($x)} ); my $f = $F[shift]; my $M = shift; my ($a, $b) = (0.0, 1.0); my ($res, $begin, $end, $avg); for (my $N=1000; $N<100000; $N+=1000) { $begin = [gettimeofday()]; for my $i (1..$M) { $res = integrate($a,$b,$f,$N); } $avg = tv_interval($begin)/$M; print "$res $avgn"; } 11
  • 12. A.6 PDL implementation #!/usr/bin/perl use strict; use warnings; use PDL; use Time::HiRes qw(gettimeofday tv_interval); sub integrate { my ($a, $b, $f_ref, $n) = @_; my $h = ($b-$a)/($n-1.0); my $vals = $f_ref->($h*xvals($n)); return $h*(sum($vals) - 0.5*$vals->index(0) - 0.5*$vals->index($n-1)); } die unless @ARGV == 2; my @F = ( sub {shift; }, sub {my $x=shift; $x*$x; }, sub {my $x=shift; cos($x*$x)*sin($x)} ); my $f = $F[shift]; my $M = shift; my ($a, $b) = (0.0, 1.0); my ($res, $begin, $avg); for(my $N=1000; $N<100000; $N+=1000) { $begin = [gettimeofday()]; for my $i (1..$M) { $res = integrate($a,$b,$f,$N); } $avg = tv_interval($begin)/$M; print "$res $avgn"; } 12