Speed-up for loops
bartc at freeuk.com
Fri Sep 3 22:17:44 CEST 2010
"Michael Kreim" <michael at perfect-kreim.de> wrote in message
news:mailman.362.1283422325.29448.python-list at python.org...
> I was comparing the speed of a simple loop program between Matlab and
> My Codes:
> $ cat addition.py
> imax = 1000000000
> a = 0
> for i in xrange(imax):
> a = a + 10
> print a
> Unfortunately my Python Code was much slower and I do not understand why.
> Are there any ways to speed up the for/xrange loop?
> Or do I have to live with the fact that Matlab beats Python in this
I'm not sure the Python developers were interested in getting fast loops.
For-loops which iterate between two numbers are amongst the easiest things
to make fast in a language. Yet originally you had to use:
for i in range(N):
which (if I understood correctly) actually created a list of N objects,
populated it with the values 0, 1, 2...N-1 (presumably using a more sensible
loop), then iterated between the values of the list!
So Python had the distinction of being one of the slowest languages in which
to do nothing (ie. running an empty loop).
More information about the Python-list