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. 10 5241
At Wednesday 13/12/2006 21:07, Chao wrote:
>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
On Wed, Dec 13, 2006 at 04:07:20PM -0800, Chao wrote:
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)
iD8DBQFFgKK8aIeIEqwil4YRAt8uAKCoEAZAZpIpv8aQ9JVNDl s5yVBH5gCeL4WD
/48x4VpPTrW06O3jjQxSnV0=
=81y7
-----END PGP SIGNATURE-----
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
Andrew Sackville-West wrote:
On Wed, Dec 13, 2006 at 04:07:20PM -0800, Chao wrote:
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
--7AUc2qLy4jB3hD7Z
Content-Type: application/pgp-signature
Content-Disposition: inline;
filename="signature.asc"
Content-Description: Digital signature
X-Google-AttachSize: 190
On Wednesday 13 December 2006 18:07, Chao wrote:
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
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'
Chao wrote:
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.
--
Roberto Bonvallet
Chao, you can also try Psyco, applied on functions, and when necessary
using its metaclass too.
Bye,
bearophile
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' be************@lycos.com wrote:
Chao, you can also try Psyco, applied on functions, and when necessary
using its metaclass too.
Bye,
bearophile
Chao wrote:
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
Chao wrote:
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.
--
Greg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Carl |
last post by:
I have been using Python for quite some time now and I love it. I use it
mainly for explorative computing and numerical prototyping, ie testing and
trying out different kinds of algorithms and...
|
by: Ali |
last post by:
How to use python in matlab?
|
by: dataangel |
last post by:
I'm a student who's considering doing a project for a Machine Learning
class on pathing (bots learning to run through a maze). The language
primarily used by the class has been Matlab. I would...
|
by: Nicolas Pernetty |
last post by:
Hello,
I'm looking for any work/paper/ressource about continuous system
simulation using Python or any similar object oriented languages (or
even UML theory !).
I'm aware of SimPy for...
|
by: Carl |
last post by:
I am desperately looking for a way to call Python from Matlab. I have become
used to Python's rich syntax and large number of libraries, and feel
ridiculously clumsy being stuck with Matlab's...
|
by: Frank-O |
last post by:
Hi ,
Recently I have been commited to the task of "translating" some complex
statistical algorithms from Matlab to C++. The goal is to be three
times as fast as matlab ( the latest) .
I've...
|
by: Allen |
last post by:
Does anyone agree with me?
If you have used Matlab, welcome to discuss it.
|
by: wang frank |
last post by:
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...
|
by: itcecsa |
last post by:
Hi,
I am implementing a small Python project, what I am going to do is to
open Matlab and run some M-files, and get some output from Matlab
command prompt.
I have no idea how to open Matlab...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |