On Thu, 16 Dec 2010 23:34:21 +0000, BartC wrote: > In terms of a more realistic function (admittedly still a little > contrived, as the loop would be written differently), I tried this: > > def p2(n): > p=1 > while True: > if n<=p: return p > p<<=1 > return 0 > > for i in xrange(1000000): > x=p2(i) > > p2() calculates the smallest power of 2 >= it's operand. > > Using while True as shown, it took 3.4 seconds. Using While 1, it took > 2.6 seconds (Python 2.5).
Right. And a saving of 0.8 microseconds per iteration is a micro- optimization which is likely to be invisible in any real situation. I mean, yes, you saved almost an entire second. Wow. Save another 179 of them and you'll almost have enough time to make yourself a coffee. Bart, we get it. Nobody denies that the optimization is real, only that it is generally meaningful. Who cares whether it takes 2 seconds or 4 seconds to generate one million results if the rest of the application takes 3 minutes to run? *If* your application is such that saving 0.8 microseconds per iteration actually is useful, AND your loop has to be written as a while True loop, then this *may* be a useful micro-optimization to save 0.8 microseconds per iteration. That's a vanishingly tiny proportion of all code written. If your code happens to meet those conditions, then by all means use "while 1". Or move to Python 3, where "while True" has the same optimization performed. But in general, such micro-optimizations are not terribly useful. If you shave off 1 second off a program that runs in 3 seconds, chances are nobody is even going to notice. Two seconds or three, who cares? Either way, it's too short to do anything else, and not long enough to matter. If you shave off an hour off a program that takes 20 hours, who is going to care? But so long as it doesn't introduce bugs, or make maintenance harder, or add complexity, such micro-optimizations don't harm either. -- Steven -- http://mail.python.org/mailman/listinfo/python-list