[Tutor] Efficiency of while versus (x)range
alan.gauld at btinternet.com
Thu Mar 17 02:42:42 CET 2011
"Shane O'Connor" <spiderbabymass at gmail.com> wrote
> First-time poster here. I've a question about loop efficiency - I
> wondering whether this code:
> i = 0
> while i < 1000:
> do something
> is more efficient than:
> for i in range(1000):
> do something
It is impossible to tell and 99% of the time irrelevant since
the body of the loop will be far more significant than the
In the case of Python its impossible to tell without profiling
and looking at the generated byte code. With true copmiled
languages it is even harder because an optimising compiler
will take account of the body of the loop and concert
between while and for type structures as appropriate.
So for some cases the for will be faster and for others the
while will be. I can honestly say I have never, in around 30
years of programming, converted a 'while' to a 'for' or vice
versa as a performance improvement, the difference is
so tiny as to be irrelevant. If that will make a difference you
are using the wrong language and you should probably
rewrite the function in C or assembler.
> In my mind, the while loop should not allocate as much memory as
> range or
> have the overhead of the iterator of xrange
But on the other hand it has to do the counter indexing and
incrementing using pure python rather than in C.
When in doubt profile it.
> Is this the case or does the compiler do something clever here?
Who knows and different Python implementations might
do it differently. And different versions of Python might
give different results. There is no way to tell in any general
> In particular, I'm using Python 2.4.3 on a web server
> which needs to run as fast as possible using as little
> memory as possible (no surprises there!).
As a general rule you could say that about any program.
But in practice web apps are far more performance
constrained by the amount of HTML and other stuff they
have to output plus the time spent accessing databases
and then waiting for the network to respond. Very few
web apps run anywhere near 100% execution efficiency,
they will usually be I/O bound. Moving things into or out of
execution) will likely be far more significant improvements
than worrying about the difference between a 'for' or 'while'
> I'm aware that there are more significant optimizations
> than the above
Yes, orders of magnitude more significant. As I say if you
really are running so close that changing the loop type
makes a difference then you really need a different
programming language (or just buy a bigger box!)
> will profile the code rather than prematurely optimize
> loops at the sake of readability/errors
Optimising loops is very important when tuning code, but
that usually means addressing the algorithm and logic
inside the loop and ensuring there are no wasted/duplicated
operations, not worrying about whether it's a while or for
construct. Those are primarily readability, reliability and
maintenance issues, not performance ones.
> but I'm still curious about the answer.
Curiosity is fine, so profile it.
But do it twice. Once with a dummy loop body (not an
empty one but one with a very simple operation that
can be duplicated for both types) Then do it again with a
realistic loop body, something containg an if/else branch
plus several operations. Then consider how much
difference the loop type really makes. (prime numbers
or fibbonacci series type problems are fine)
Author of the Learn to Program web site
More information about the Tutor