Stargaming wrote:

> Alex Martelli schrieb:
>> 7stud <[EMAIL PROTECTED]> wrote:
>>    ...
>> 
>>>>.append - easy to measure, too:
>>>>
>>>>brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
>>>>1000000 loops, best of 3: 1.31 usec per loop
>>>>
>>>>brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]'
>>>>1000000 loops, best of 3: 1.52 usec per loop
>>>>
>>>>Alex
>>>
>>>Why is it necessary to copy L?
>> 
>> 
>> If you don't, then L gets longer and longer -- to over a million
>> elements by the end of the loop -- so we're measuring something that's
>> potentially very different from the problem under study, "what's the
>> best way to append one item to a 3-items list".
>> 
>> That's important to consider for any microbenchmark of code that changes
>> some existing state: make sure you're measuring a piece of code that
>> _overall_ does NOT change said existing state in a cumulative way,
>> otherwise you may be measuring something very different from the issue
>> of interest.  It's maybe the only important caveat about using "python
>> -mtimeit".
>> 
>> 
>> Alex
>>  
> 
> Cannot reproduce that. Consider the following:
> 
> $ python -mtimeit "L=range(3)" "L.append(1); print len(L)"

That should be

$ python2.5 -m timeit -s "L = range(3)" "L.append(1); print len(L)" | tail
999995
999996
999997
999998
999999
1000000
1000001
1000002
1000003
1000000 loops, best of 3: 1.98 usec per loop

Note the -s before the initialization statement that Alex meant to add but
didn't. If that is missing 

L = range(3) 

is executed on every iteration.

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to