speed of python vs matlab.

C

Chao

I've been trying to develop some numerical codes with python, however
got disappointed.

A very simple test,

a = 1.0

for i in range(1000):
for j in range(1000):
a = a+1

unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
3.0G, 1G RAM, it varies according to machine configuration, but should
be in the same level)

for matlab, the same operation took 0.1 seconds,

I use numpy & scipy, they solve the problem most of the times, but
there are cases you can't avoid loops by vectors. I appreciate the
elegancy of python so much, but I guess I have to gave it up in these
numerical codes.(image processing algorithms), for application
dev/scripting, it's still my first choice.

A good news is that the same code takes ruby 9.8 seconds.
 
G

Gabriel Genellina

I've been trying to develop some numerical codes with python, however
got disappointed.

A very simple test,

a = 1.0

for i in range(1000):
for j in range(1000):
a = a+1

unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
3.0G, 1G RAM, it varies according to machine configuration, but should
be in the same level)

How do you measure it? 4.5 secs is far too much. Anyway, try using
xrange instead of range. This is the standard way to do timings:

--- cut ---
def test():
a = 1.0
for i in xrange(1000):
for j in xrange(1000):
a = a+1

if __name__=='__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.repeat(repeat=3,number=1)
--- cut ---

I got about 0.24 secs with far less hardware.

For vector-oriented operations the NumArray package is well suited.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
A

Andrew Sackville-West

I've been trying to develop some numerical codes with python, however
got disappointed.

A very simple test,

a = 1.0

for i in range(1000):
for j in range(1000):
a = a+1

unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
3.0G, 1G RAM, it varies according to machine configuration, but should
be in the same level)

somethings not right there.

andrew@debian:~$ cat pytimetest.py
a=1.0
for i in range (1000):
for j in range (1000):
a=a+1


andrew@debian:~$ time python pytimetest.py

real 0m0.534s
user 0m0.528s
sys 0m0.000s


andrew@debian:~$ cat /proc/cpuinfo | grep name
model name : Intel(R) Celeron(R) CPU 2.53GHz

andrew@debian:~$ uname -a
Linux debian 2.6.18-3-686 #1 SMP Mon Dec 4 16:41:14 UTC 2006 i686
GNU/Linux

A

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFgKK8aIeIEqwil4YRAt8uAKCoEAZAZpIpv8aQ9JVNDls5yVBH5gCeL4WD
/48x4VpPTrW06O3jjQxSnV0=
=81y7
-----END PGP SIGNATURE-----
 
C

Chao

My Bad, the time used by python is 0.46~0.49 sec,
I tried xrange, but it doesn't make things better.

import time
tic = time.time()
a = 1.0

array = range(1000)

for i in array:
for j in array:
a = a + 0.1

toc = time.time()
print toc-tic,' has elapsed'

used by matlab is 0.012sec

tic
a = 1;
for i=1:1000
for j=1:1000
a = a + 1;
end
end
toc

used by ruby is 0.94~0.96sec

a = 1
start = Time.now()

1000.times do
1000.times do
a = a + 1
end
end

finish = Time.now()

puts finish - start
 
J

Jonathan Curran

I've been trying to develop some numerical codes with python, however
got disappointed.

A very simple test,

a = 1.0

for i in range(1000):
for j in range(1000):
a = a+1

unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
3.0G, 1G RAM, it varies according to machine configuration, but should
be in the same level)

for matlab, the same operation took 0.1 seconds,

I use numpy & scipy, they solve the problem most of the times, but
there are cases you can't avoid loops by vectors. I appreciate the
elegancy of python so much, but I guess I have to gave it up in these
numerical codes.(image processing algorithms), for application
dev/scripting, it's still my first choice.

A good news is that the same code takes ruby 9.8 seconds.

[icicled@A3200 ~]$ time python foo # where foo contained your exact code

real 0m0.469s
user 0m0.443s
sys 0m0.017s

4.5 seconds? ouch. I've got somewhere near 1 second. Something sounds a little
fishy b/c my machine is an AMD 3200+ (2.2GHz) w/ 1GB RAM. Yours is a lot
faster in terms of clock speed.

Anyway, do take a look at some of the available python compilers. They should
help considerably.

- Jonathan
 
D

Dennis Lee Bieber

unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
3.0G, 1G RAM, it varies according to machine configuration, but should
be in the same level)
Something is wrong...

My machine at work is same specs...

-=-=-=-=-=-
# loop test
##
##d:\data\dbieber\My Documents>python Script1.py
##time average xrange: 0.171831910845
##time average range: 0.167142064527
##
##d:\data\dbieber\My Documents>
##
# 3GHz P4, 1GB RAM

import time

def cycle():
a = 1.0
for i in xrange(1000):
for j in xrange(1000):
a = a + 1
return a

x = 0
start = time.clock()
for t in xrange(10):
x = x + cycle()
end = time.clock()

print "time average xrange: ", (end - start) / 10

def cycle2():
a = 1.0
for i in range(1000):
for j in range(1000):
a = a + 1
return a

x = 0
start = time.clock()
for t in xrange(10):
x = x + cycle2()
end = time.clock()

print "time average range: ", (end - start) / 10
-=-=-=-=-=-

My home machine is 3.4GHz, 2GB, P4 HT

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Dennis Lee Bieber>e:

E:\>cd "UserData\Dennis Lee Bieber\My Documents"

E:\UserData\Dennis Lee Bieber\My Documents>python script11.py
time average xrange: 0.152818990936
time average range: 0.16620539271

E:\UserData\Dennis Lee Bieber\My Documents>
for matlab, the same operation took 0.1 seconds,

Looks like on my machine, the Python is only 50% slower than your
matlab.

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
C

Christophe

Chao a écrit :
My Bad, the time used by python is 0.46~0.49 sec,
I tried xrange, but it doesn't make things better.

import time
tic = time.time()
a = 1.0

array = range(1000)

for i in array:
for j in array:
a = a + 0.1

toc = time.time()
print toc-tic,' has elapsed'

Place all your code inside functions please. IIRC, local variable access
is much faster that way, and you do a lot of lookup for the a local
variable in that code.


import time

def main():
a = 1.0

array = range(1000)

for i in array:
for j in array:
a = a + 0.1

tic = time.time()
main()
toc = time.time()
print toc-tic,' has elapsed'
 
R

Roberto Bonvallet

Chao said:
My Bad, the time used by python is 0.46~0.49 sec,
I tried xrange, but it doesn't make things better.

Actually it does: it doesn't waste time and space to create a big list.
 
B

bearophileHUGS

Chao, you can also try Psyco, applied on functions, and when necessary
using its metaclass too.

Bye,
bearophile
 
C

Chao

Thank you guys for your interest,

I tried two things 1) put code into a function 2) use psyco.

1) by putting them into a function, there is a significant improvement,
around 30%
the running time will be around 0.3sec

2) by using psyco, it really does a great job, the running time is
around 0.045sec.

While trying this another question comes up,
psyco seems to be able to optimize built-in functions & user's code, if
I call a function from an external library, it seems doesn't help.
A simple thing is I placed a = numpy.sin(a) in the loop rather than a =
a+1, in this case,
psyco doesn't have any improvement(or very little). if I put a =
math.sin(a) which is from an built-in function, it can achieve a
improvement around 3~4. Could the reason be that numpy.sin is
actually calling a C library ?

Actually Python does show comparable/better performance than other
scripting languages. but I'm just surprised that matlab does a great
job compared to python/perl, since matlab is also a interpreted
language, I'm expecting it has silimar performance with python.

I did some search, in previous discussion, people has compared
python/numpy vs matlab,
but it is actually comparison between numpy(which is implemented in c)
vs matlab.

Chao.

import psyco
#psyco.bind(functest)
psyco.full()

import numpy
import time,math

def functest(a):
array = xrange(1000)

for i in array:
for j in array:
a = a + 1

tic = time.time()

a = 1.0
functest(a)

toc = time.time()
print toc-tic,' has elapsed'
 
R

Robert Kern

Chao said:
While trying this another question comes up,
psyco seems to be able to optimize built-in functions & user's code, if
I call a function from an external library, it seems doesn't help.
A simple thing is I placed a = numpy.sin(a) in the loop rather than a =
a+1, in this case,
psyco doesn't have any improvement(or very little). if I put a =
math.sin(a) which is from an built-in function, it can achieve a
improvement around 3~4. Could the reason be that numpy.sin is
actually calling a C library ?

The reason for the difference is that psyco recognizes math.sin() and replaces
it with equivalent machine code to call the standard C library function sin().
It does not recognize numpy.sin(), and it is implemented in C, not Python, so it
does not do optimization.
Actually Python does show comparable/better performance than other
scripting languages. but I'm just surprised that matlab does a great
job compared to python/perl, since matlab is also a interpreted
language, I'm expecting it has silimar performance with python.

Matlab uses a JIT compiler along the lines of psyco for simple operations like
the one you are doing.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

greg

Chao said:
I did some search, in previous discussion, people has compared
python/numpy vs matlab,
but it is actually comparison between numpy(which is implemented in c)
vs matlab.

Yes, matlab is operating on whole arrays at a time,
like numpy. So it's not surprising that they have
comparable performance.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top