loop in python

Peter Hansen peter at engcorp.com
Mon Aug 22 18:23:32 CEST 2005


km wrote:
> ya i am sorry  i tried with an empty loop first and then one which 
 > emits a value as the snippet. I have tested it on my machine
 > and now ...
> 
> what more do i need to accept python is slow when it comes to 
 > loops concept ?

You've sort of missed some of the points being made, which in essence 
are saying "your benchmark is less than useless, and you're confused 
about what matters".

One key point about benchmarking is to make the test representative.  If 
you are going to be running empty loops, then you're doing the right 
thing.  If *all* your loops are going to do is print stuff, then you're 
doing the right thing with the version that "emits values".

On the other hand, if you are really doing something else entirely, then 
your tests are telling you nothing about your real problem, and are in 
fact confusing you greatly, which is why they are less than useless.

With the part about "confused about what matters" I mean that you are 
trying to optimize something a) when you don't have working code, and b) 
when you don't know that you need to make it any faster.

The first rule of optimization is to make your code work first, and only 
then consider making it fast.  The second rule of optimization is that 
you shouldn't waste time doing it unless you really need faster results 
(and it requires actual elapsed time or response time measurements to 
know this).  Since you haven't got any working code, it's not possible 
that you *need* whatever negligible speed difference there might be 
between Python and Perl.

And, lastly, in case this thought didn't get through either: Python is 
*not* significantly slower than Perl except in certain uncommon cases -- 
perhaps including useless empty loops -- so if you really want to use 
Python, don't let your first attempts at benchmarking dissuade you. 
Really, trust us.

Python's strengths lie in four things: the readability of the code, the 
huge range of library modules available, the elegance of its object 
oriented constructs, and the helpfulness of its community.  Raw speed is 
not one of its strengths, but there are tens of thousands of people 
using it quite effectively and without daily concern for its speed (same 
as Perl, by the way since, again, they are _not_ significantly different 
in speed no matter what an empty loop test shows).

-Peter



More information about the Python-list mailing list