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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to