SlideShare a Scribd company logo
1 of 12
Download to read offline
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

Yampa AFRP Introduction
Yampa AFRP IntroductionYampa AFRP Introduction
Yampa AFRP IntroductionChengHui Weng
 
LeFlowを調べてみました
LeFlowを調べてみましたLeFlowを調べてみました
LeFlowを調べてみましたMr. Vengineer
 
A Simple Communication System Design Lab #4 with MATLAB Simulink
A Simple Communication System Design Lab #4 with MATLAB SimulinkA Simple Communication System Design Lab #4 with MATLAB Simulink
A Simple Communication System Design Lab #4 with MATLAB SimulinkJaewook. Kang
 
190111 tf2 preview_jwkang_pub
190111 tf2 preview_jwkang_pub190111 tf2 preview_jwkang_pub
190111 tf2 preview_jwkang_pubJaewook. Kang
 
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0Databricks
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonRoss McDonald
 
Tail call optimization (TCO) - Lecture
Tail call optimization (TCO) - LectureTail call optimization (TCO) - Lecture
Tail call optimization (TCO) - LectureJesse Frankley
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06Niit Care
 
Use of an Oscilloscope - maXbox Starter33
Use of an Oscilloscope - maXbox Starter33Use of an Oscilloscope - maXbox Starter33
Use of an Oscilloscope - maXbox Starter33Max Kleiner
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16Max Kleiner
 
What’s eating python performance
What’s eating python performanceWhat’s eating python performance
What’s eating python performancePiotr Przymus
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage CollectionEelco Visser
 
Introduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasIntroduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasOswald Campesato
 
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from..."PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...Edge AI and Vision Alliance
 
High Performance Python - Marc Garcia
High Performance Python - Marc GarciaHigh Performance Python - Marc Garcia
High Performance Python - Marc GarciaMarc Garcia
 
Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)Oswald Campesato
 

What's hot (20)

Yampa AFRP Introduction
Yampa AFRP IntroductionYampa AFRP Introduction
Yampa AFRP Introduction
 
Using Parallel Computing Platform - NHDNUG
Using Parallel Computing Platform - NHDNUGUsing Parallel Computing Platform - NHDNUG
Using Parallel Computing Platform - NHDNUG
 
LeFlowを調べてみました
LeFlowを調べてみましたLeFlowを調べてみました
LeFlowを調べてみました
 
A Simple Communication System Design Lab #4 with MATLAB Simulink
A Simple Communication System Design Lab #4 with MATLAB SimulinkA Simple Communication System Design Lab #4 with MATLAB Simulink
A Simple Communication System Design Lab #4 with MATLAB Simulink
 
190111 tf2 preview_jwkang_pub
190111 tf2 preview_jwkang_pub190111 tf2 preview_jwkang_pub
190111 tf2 preview_jwkang_pub
 
mpi4py.pdf
mpi4py.pdfmpi4py.pdf
mpi4py.pdf
 
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
 
Brief Introduction to Cython
Brief Introduction to CythonBrief Introduction to Cython
Brief Introduction to Cython
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPython
 
Tail call optimization (TCO) - Lecture
Tail call optimization (TCO) - LectureTail call optimization (TCO) - Lecture
Tail call optimization (TCO) - Lecture
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06
 
Concurrency with Go
Concurrency with GoConcurrency with Go
Concurrency with Go
 
Use of an Oscilloscope - maXbox Starter33
Use of an Oscilloscope - maXbox Starter33Use of an Oscilloscope - maXbox Starter33
Use of an Oscilloscope - maXbox Starter33
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16
 
What’s eating python performance
What’s eating python performanceWhat’s eating python performance
What’s eating python performance
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 
Introduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasIntroduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and Keras
 
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from..."PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
 
High Performance Python - Marc Garcia
High Performance Python - Marc GarciaHigh Performance Python - Marc Garcia
High Performance Python - Marc Garcia
 
Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)
 

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

Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)Qiangning Hong
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxPJS KUMAR
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4Max Kleiner
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithmssangeetha s
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014PyData
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
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
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerMarina Kolpakova
 
C optimization notes
C optimization notesC optimization notes
C optimization notesFyaz Ghaffar
 
Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithmPratik Mota
 
Subtle Asynchrony by Jeff Hammond
Subtle Asynchrony by Jeff HammondSubtle Asynchrony by Jeff Hammond
Subtle Asynchrony by Jeff HammondPatrick Diehl
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11Henry Schreiner
 
Tackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in RTackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in RLun-Hsien Chang
 
Rapport_Cemracs2012
Rapport_Cemracs2012Rapport_Cemracs2012
Rapport_Cemracs2012Jussara F.M.
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 

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

C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptx
 
cp05.pptx
cp05.pptxcp05.pptx
cp05.pptx
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
C optimization notes
C optimization notesC optimization notes
C optimization notes
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Mpi in-python
Mpi in-pythonMpi in-python
Mpi in-python
 
Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithm
 
Subtle Asynchrony by Jeff Hammond
Subtle Asynchrony by Jeff HammondSubtle Asynchrony by Jeff Hammond
Subtle Asynchrony by Jeff Hammond
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
 
Tackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in RTackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in R
 
Rapport_Cemracs2012
Rapport_Cemracs2012Rapport_Cemracs2012
Rapport_Cemracs2012
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 

Recently uploaded

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

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