My (late) beef with Simple Generator syntax (PEP 255)

Gerhard Häring gerhard.haering at opus-gmbh.net
Fri Nov 15 06:03:13 EST 2002


In article <jpVA9.14198$Yw.601501 at news2.tin.it>, Alex Martelli wrote:
> Neil Schemenauer wrote:
>> Just wrote:
>>> IMO the best idiom for this currently is
>>> 
>>>   def foo():
>>>       while 0:
>>>           yield "whatever"
>> 
>> This whole discussion is silly, IMHO.  The best idiom is:
>> 
>>     def foo():
>>         return []
> 
> Actually, I suspect returning () instead -- empty tuple, not
> empty list -- is even better.  To wit:
>
> [snip proof] 
>
> admittedly the difference is going to be even less in 2.3,
> but still, it's about 100 milliseconds per million empties one
> returns -- a few billions, and we'd be talking about REAL
> performance issues.  Surely any implementation of something
> as frequent as "returning an empty iterable" cannot aspire
> to the title of "the best" with that sort of performance hit.

Yeah, it's quite likely that this will be the bottleneck of your app. That's
why you should squeeze the last percent or so of optimization out and return an
empty string:

[Python 2.2.2/win32]
D:\tmp>python ba.py
emptylist 1.71
emptytuple 1.16
emptystring 1.15
emptylist 1.72
emptytuple 1.16
emptystring 1.15
emptylist 1.72
emptytuple 1.16
emptystring 1.15

No /dev/emoticon on Windows, either.
-- 
Gerhard Häring
OPUS GmbH München
Tel.: +49 89 - 889 49 7 - 32
http://www.opus-gmbh.net/



More information about the Python-list mailing list