speed of python vs matlab.

Gabriel Genellina gagsl-py at yahoo.com.ar
Wed Dec 13 19:53:12 EST 2006


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



More information about the Python-list mailing list