Speed of Python

W

wang frank

Hi,

While comparing the speed of octave and matlab, I decided to do a similar
test for python and matlab. The result shows that python is slower than
matlab by a factor of 5. It is not bad since octave is about 30 time slower
than matlab.

Here is the result in matlab:
Elapsed time is 0.015389 seconds.

and in Python:[0.071012377266015392]

Here is the bench1.py:
import math
def bench1(n):
for i in range(n):
for j in range(1000):
m=j+1
z=math.log(m)
z1=math.log(m+1)
z2=math.log(m+2)
z3=math.log(m+3)
z4=math.log(m+4)
z5=math.log(m+5)
z6=math.log(m+6)
z7=math.log(m+7)
z8=math.log(m+8)
z9=math.log(m+9)
return z9

Is my conclusion correct that Python is slower than matlab? Are there any
way to speed it up? It seems Python automatically created bench1.pyc. Does
Python automatically execute the bench1.pyc to speed it up?


Thanks

Frank

_________________________________________________________________
$B!VCO?^%^%,!WFC=8(B $B;D=k$r?a$-$H$P$;!*$4EvCO%"%$%9%/%j!<%`%^%C%W$,EP>l(B
http://chizumaga.jp/
 
S

S bastien Boisg rault

Matlab (aka MATrix LABoratory) has been designed with numeric
computations in mind (every object being natively a n-dim
array). If you wish to develop that kind of applications in
Python, consider using the numerical array structure provided
by Numpy. Numpy being mostly written in C, that should speed
up your application ... In this context, additional 3rd party
libs such as Scipy, Matplotlib, PIL, etc. will also be useful.

Cheers,

SB
Hi,

While comparing the speed of octave and matlab, I decided to do a similar
test for python and matlab. The result shows that python is slower than
matlab by a factor of 5. It is not bad since octave is about 30 time slower
than matlab.

Here is the result in matlab:
Elapsed time is 0.015389 seconds.

and in Python:>>> t=timeit.Timer("bench1.bench1(10)","import bench1")
[0.071012377266015392]

Here is the bench1.py:
import math
def bench1(n):
for i in range(n):
for j in range(1000):
m=j+1
z=math.log(m)
z1=math.log(m+1)
z2=math.log(m+2)
z3=math.log(m+3)
z4=math.log(m+4)
z5=math.log(m+5)
z6=math.log(m+6)
z7=math.log(m+7)
z8=math.log(m+8)
z9=math.log(m+9)
return z9

Is my conclusion correct that Python is slower than matlab? Are there any
way to speed it up? It seems Python automatically created bench1.pyc. Does
Python automatically execute the bench1.pyc to speed it up?

Thanks

Frank

_________________________________________________________________
$B!VCO?^%^%,!WFC=8(B $B;D=k$r?a$-$H$P$;!*$4EvCO%"%$%9%/%j!<%`%^%C%W$,EP>l(Bhttp://chizumaga.jp/
 
R

Roberto Bonvallet

Here is the bench1.py:
import math
def bench1(n):
for i in range(n):
for j in range(1000):
m=j+1
z=math.log(m)
z1=math.log(m+1)
z2=math.log(m+2)
z3=math.log(m+3)
z4=math.log(m+4)
z5=math.log(m+5)
z6=math.log(m+6)
z7=math.log(m+7)
z8=math.log(m+8)
z9=math.log(m+9)
return z9

Is my conclusion correct that Python is slower than matlab?

Show us your Matlab code in order to see if both are equivalent.
Your Python code creates n lists of 1000 elements, so you're not
actually
measuring only the numeric computations.

Cheers,
 
R

Roberto Bonvallet

Hi,
Here is the matlab code:
function [z]=bench1(n)
for i=1:n,
for j=1:1000,
z=log(j);
z1=log(j+1);
z2=log(j+2);
z3=log(j+3);
z4=log(j+4);
z5=log(j+5);
z6=log(j+6);
z7=log(j+7);
z8=log(j+8);
z9=log(j+9);
end
end
z = z9;

I am not familiar with python, so I just simply try to reproduce the same
code in python.
If you think that my python script is not efficient, could you tell me how
to make it more efficient?

The range(1000) call creates a list of 1000 elements each time it is
called.
This is expensive. You could try something like the following:

def bench1a(n):
r = range(1000)
for i in range(n):
# reuse the list
for j in r:
...

def bench1b(n):
for i in range(n):
# don't use a list at all
j = 0
while j < 1000:
...

I'm no expert on Python optimization either, so I cannot guarantee
that both are
the best way to write this algorithm.

This step also doesn't occur in the Matlab code.

Hope this helps, although maybe I'm not the right person to talk about
optimization, and I haven't measured my code.
 
G

George Sakkis

Is my conclusion correct that Python is slower than matlab? Are there any
way to speed it up?

Yes, use Numpy for any non trivial number-crunching task: http://numpy.scipy.org/.
Even if you don't, you can speed up the original function by ~25% if
you replace range() with xrange() and bind the log function to a local
variable:

def bench2(n):
from math import log
for i in xrange(n):
for j in xrange(1000):
m=j+1
z=log(m)
z1=log(m+1)
z2=log(m+2)
z3=log(m+3)
z4=log(m+4)
z5=log(m+5)
z6=log(m+6)
z7=log(m+7)
z8=log(m+8)
z9=log(m+9)
return z9


HTH,

George
 
I

Istvan Albert

Is my conclusion correct that Python is slower than matlab?

There are ways to speed up code execution, but to see substantial
improvement you'll need to use numpy and rework the code to operate
on vectors/matrices rather than building the result one step at the
time. This applies to Octave as well. See the example code at the end
of the message.

With that code computing 1 million logarithms showed the following
tendency

original => 648.972728 msec per pass
optimized => 492.613773 msec per pass
with numpy => 120.578616 msec per pass

The "slowness of python in this example mainly comes from the function
call (math.log) as it seems about 30% of the runtime is spent calling
the function.

import timeit

setup = """
import math
from numpy import arange, log
size = 1000
"""

code1 = """
#original code
for i in range(size):
for j in range(size):
a = math.log(j+1)
"""

code2 = """
# minor improvements lead to 15% faster speed
from math import log
for i in xrange(size):
for j in xrange(size):
a = log(j+1)
"""

code3 = """
# applying via a universal function makes it 5 times faster
for i in xrange(size):
nums = arange( size )
a = log( nums + 1)
"""

N = 3
codes = [ code1, code2, code3 ]

for stmt in codes:
timer = timeit.Timer(stmt=stmt, setup=setup)
msec = 1000.0 * timer.timeit(number=N)/N
print "%f msec per pass" % msec
 
A

ajaksu

I am not familiar with python, so I just simply try to reproduce the same
code in python.
Seems almost correct, but from what I guess of MatLab, George's
suggestions make it a bit more fair.
If you think that my python script is not efficient, could you tell me how
to make it more efficient?
In pure Python? No idea (besides using Roberto's and George's
suggestions). If you allow for extensions, Istvan has the answer. If
you allow compiling Python to C++ (using ShedSkin: http://shed-skin.blogspot.com/),
here's a small report:

----------
ajaksu@Belkar:~/sandbox$ cat bench.py
import math
n = 1
def bench1(n):
for i in range(n):
for j in range(1000):
m=j+1
z=math.log(m)
z1=math.log(m+1)
z2=math.log(m+2)
z3=math.log(m+3)
z4=math.log(m+4)
z5=math.log(m+5)
z6=math.log(m+6)
z7=math.log(m+7)
z8=math.log(m+8)
z9=math.log(m+9)
return z9
a = bench1(10)

ajaksu@Belkar:~/sandbox$ ss -e bench.py
*** SHED SKIN Python-to-C++ Compiler 0.0.22 ***
Copyright 2005-2007 Mark Dufour; License GNU GPL version 2 (See
LICENSE)
(Please send bug reports here: (e-mail address removed))

[iterative type analysis..]
**
iterations: 2 templates: 44
[generating c++ code..]
ajaksu@Belkar:~/sandbox$ make bench.so
g++ -O3 -s -pipe -fomit-frame-pointer -I/home/ajaksu/shedskin-0.0.22/
lib -g -fPIC -I/usr/include/python2.5 -D__SS_BIND /home/ajaksu/
shedskin-0.0.22/lib/builtin.cpp /home/ajaksu/shedskin-0.0.22/lib/
math.cpp bench.cpp -lgc -shared -Xlinker -export-dynamic -lpython2.5 -
o bench.so

ajaksu@Belkar:~/sandbox$ mv bench.py pbench.py

ajaksu@Belkar:~/sandbox$ ipython
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[...]
In [1]: from pbench import bench1 as pbench1

In [2]: from bench import bench1

In [3]: %timeit a = bench1(10)
100 loops, best of 3: 10.2 ms per loop

In [4]: %timeit a = pbench1(10)
10 loops, best of 3: 92.8 ms per loop

----------

I guess you'd also see nice improvements from Pyrex or Cython, Blitz
and other tools. Check http://wiki.python.org/moin/PythonSpeed/PerformanceTips
for the general ideas and http://scipy.org/PerformancePython for an
insight on available tools that even compares their speeds to Matlab.

And-if-you-run-more-benchmarks-please-do-post-them-ly yrs,
Daniel
 
T

Terry Reedy

| z=log(j);

This matlab code is faster in part than your Python equivalent:

| > > z=math.log(m)

because of the repeated lookup of log in the math module.
So, replace

| > > import math

with

from math import log

Also make that the top line of the function so 'log' is a local variable.
Then delete 'math.' everywhere.

That said, such artificial benchmarks do not strike me as terribly useful.
More useful, for instance, for appropriate applications, might be doing a
fourier transform on a 2**20 length vector with both matlab and numpy/scipy

tjr
 
C

Carl Banks

Hi,

While comparing the speed of octave and matlab, I decided to do a similar
test for python and matlab. The result shows that python is slower than
matlab by a factor of 5. It is not bad since octave is about 30 time slower
than matlab.

Here is the result in matlab:
Elapsed time is 0.015389 seconds.

and in Python:>>> t=timeit.Timer("bench1.bench1(10)","import bench1")
[0.071012377266015392]

Here is the bench1.py:
import math
def bench1(n):
for i in range(n):
for j in range(1000):
m=j+1
z=math.log(m)
z1=math.log(m+1)
z2=math.log(m+2)
z3=math.log(m+3)
z4=math.log(m+4)
z5=math.log(m+5)
z6=math.log(m+6)
z7=math.log(m+7)
z8=math.log(m+8)
z9=math.log(m+9)
return z9

Is my conclusion correct that Python is slower than matlab?


Whoa, there, chief, that's a pretty lofty conclusion to make based on
one benchmark.

A blanket speed comparison between Matlab and Python isn't productive:
they each have their own strengths speedwise.

(Conversely, a blanket comparison is productive programmingwise.
Matlab has almost no strengths relative to Python in that department.
But that's another question entirely. :)

Roughly speaking, I'd say your average single-pass calculation script
is going to be faster in Matlab than in Python. However, Python (with
help from numpy) has more opportunities for optimization, especially
if you're using large matrices. And if you need to take it up a
notch, Python has very good ways to integrate numerical C and Fortran
code.

Are there any
way to speed it up? It seems Python automatically created bench1.pyc. Does
Python automatically execute the bench1.pyc to speed it up?

Yes, as others have explained.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top