A Python 3000 Question

Neil Cerutti horpner at yahoo.com
Wed Oct 31 09:33:04 EDT 2007


On 2007-10-31, George Sakkis <george.sakkis at gmail.com> wrote:
> On Oct 31, 8:44 am, Neil Cerutti <horp... at yahoo.com> wrote:
>> On 2007-10-30, George Sakkis <george.sak... at gmail.com> wrote:
>>
>>
>>
>> > On Oct 30, 11:25 am, Neil Cerutti <horp... at yahoo.com> wrote:
>> >> On 2007-10-30, Eduardo O. Padoan <eduardo.pad... at gmail.com> wrote:
>>
>> >> > This is a FAQ:
>> >> >http://effbot.org/pyfaq/why-does-python-use-methods-for-some-function...
>>
>> >> Holy Airy Persiflage Batman!
>>
>> >> Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on
>> >> win32
>> >> Type "help", "copyright", "credits" or "license" for more information.>>> import timeit
>> >> >>> timeit.Timer('len(seq)', 'seq = range(100)').timeit()
>> >> 0.20332271187463391
>> >> >>> timeit.Timer('seq.__len__()', 'seq = range(100)').timeit()
>>
>> >> 0.48545737364457864
>>
>> > Common mistake; try this instead:
>>
>> > timeit.Timer('seqlen()',
>> >              'seq = range(100); seqlen=seq.__len__').timeit()
>>
>> Why would I want to do that?
>
> To realize that __len__ is actually faster than len in this
> case; your second timing is dominated by the time to do
> attribute lookup.

I knew that going in. Making len a builtin saves us from writing
our own seqlen functions. ;)

-- 
Neil Cerutti
He's a guy who gets up at six o'clock in the morning regardless of what time
it is. --Lou Duva



More information about the Python-list mailing list