Speed-up for loops
Peter Otten
__peter__ at web.de
Thu Sep 2 06:36:51 EDT 2010
Michael Kreim wrote:
> I was comparing the speed of a simple loop program between Matlab and
> Python.
>
> My Codes:
> $ cat addition.py
> imax = 1000000000
> a = 0
> for i in xrange(imax):
> a = a + 10
> print a
> Are there any ways to speed up the for/xrange loop?
Move it into a function; this turns a and i into local variables.
def f():
imax = 1000000000
a = 0
for i in xrange(imax):
a = a + 10
print a
f()
> Or do I have to live with the fact that Matlab beats Python in this
> example?
I think so.
More information about the Python-list
mailing list