[Tutor] Efficiency of while versus (x)range

Stefan Behnel stefan_ml at behnel.de
Thu Mar 17 08:45:43 CET 2011


Shane O'Connor, 17.03.2011 01:32:
> 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!).

Note that a web application involves many things outside of your own code 
that seriously impact the performance and/or resource requirements. 
Database access can be slow, excessively dynamic page generation and 
template engines can become a bottleneck, badly configured caching can eat 
your RAM and slow down your response times.

It seems very unlikely to me that the performance of loop iteration will 
make a substantial difference for you.


> I'm aware that there are more significant optimizations than the above and I
> will profile the code rather than prematurely optimize loops at the sake of
> readability/errors but I'm still curious about the answer.

You shouldn't be. Optimising at that level is clearly the wrong place to 
start with.

That touches on a really nice feature of Python. It's a language that 
allows you to focus strongly on your desired functionality instead of 
thinking about questionable micro optimisations all over the place. 
Optimisation is something that you should start to apply when your test 
suite is large enough to catch the bugs you introduce by doing it.

Stefan



More information about the Tutor mailing list