speed of python vs matlab.

Christophe chris.cavalaria at free.fr
Thu Dec 14 06:49:54 EST 2006


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'



More information about the Python-list mailing list