473,244 Members | 1,322 Online

# Speed of Python

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")
t.repeat(1,1)
[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/

Sep 7 '07 #1
4 2298

On Sep 7, 6:42 pm, "wang frank" <f...@hotmail.co.jpwrote:

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
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)","impor t bench1")
>t.repeat(1,1)

[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/

Sep 7 '07 #2
On Sep 7, 12:42 pm, "wang frank" <f...@hotmail.co.jpwrote:
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

Sep 7 '07 #3
On Sep 7, 12:42 pm, "wang frank" <f...@hotmail.co.jpwrote:
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

Sep 7 '07 #4
On Sep 7, 12:42 pm, "wang frank" <f...@hotmail.co.jpwrote:
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)","impor t bench1")
>t.repeat(1,1)

[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

Sep 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 34 by: Jacek Generowicz | last post by: I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args,... 4 by: EP | last post by: On questions of the suitability of Python for CGI, embedded apps, etc., execution speed comes to mind. I previously read some comparisons which did not show Python in a good light in this regard:... 29 by: Bart Nessux | last post by: Just fooling around this weekend. Wrote and timed programs in C, Perl and Python. Each Program counts to 1,000,000 and prints each number to the console as it counts. I was a bit surprised. I'm not... 28 by: Maboroshi | last post by: Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on... 1 by: | last post by: I just ran this stuff for my own knowledge. Though it might be useful to some other people to know and maybe spark a discussion. I needed a fast way to test for membership, so naturally the... 52 by: Neuruss | last post by: It seems there are quite a few projects aimed to improve Python's speed and, therefore, eliminate its main limitation for mainstream acceptance. I just wonder what do you all think? Will Python... 53 by: Krystian | last post by: Hi are there any future perspectives for Python to be as fast as java? i would like to use Python as a language for writing games. best regards krystian 11 by: Jim Lewis | last post by: Has anyone found a good link on exactly how to speed up code using pyrex? I found various info but the focus is usually not on code speedup. 11 by: kyosohma | last post by: Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While... 0 by: abbasky | last post by: ### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE... 0 by: stefan129 | last post by: Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific... 0 by: egorbl4 | last post by: Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ... 0 by: DolphinDB | last post by: The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take... 0 by: Aftab Ahmad | last post by: Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on... 0 by: ryjfgjl | last post by: ExcelToDatabase: batch import excel into database automatically... 0 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back... 0 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...