How can I optimise this? [intended in good humour]

Georg Brandl g.brandl-nospam at gmx.net
Tue Jul 25 06:49:14 EDT 2006


John Machin wrote:
> Markus wrote:
>> You know you're guilty of early/over optimisation, when it's almost two
>> in the morning and the file open in front of you reads as follows.
>>
>>   The code you are about to read is real...
>>   Some of the variable names have been changed
>>   to protect the families of those involved.
>>
>> [-snip-]
>>
>> from timeit import Timer
>>
>> if __name__=='__main__':
>>      t = Timer('len(argv)==1','from sys import argv')
>>      print "%f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
>>      t = Timer('argv[0]==argv[-1]','from sys import argv')
>>      print "%f usec/pass" % (1000000 * t.timeit(number=100000)/100000)

> Do you realise that the two expressions that you are comparing are not
> even equivalent, and moreover you ignored an expression that will be
> faster and equivalent (unless/until somebody decides on an
> "optimisation" like interning/sharing strings between/among sys.argv
> elements).

Let me point out that len(argv) == 1 is the only one that will work if
argv is []. ;) (took me a few seconds to realize I must put a smiley there)

Georg



More information about the Python-list mailing list