[PATCH] merge3 fails with python2.3

John A Meinel john at arbash-meinel.com
Wed Jul 6 02:39:43 BST 2005


Martin Pool wrote:

>On  5 Jul 2005, John A Meinel <john at arbash-meinel.com> wrote:
>
>
>
>>I think use generators wherever you can, but since you *already* have a
>>list, just use it. Is it possible for get_matching_blocks to return and
>>empty list? []
>>
>>
>
>
...

>The Python list() type is really an array, so using a index is more
>efficient than shuffling everything down to the front (though arguably
>less clear).  In this case the lists are probably so small that the
>difference would not be noticeable.
>
>
hmmm, I always thought it was a linked list, I'll have to change some of
my work. But you are correct, I did the following:

python2.4 -m timeit.py "x = range(10000)
while x:
    x.pop(0)
"
Takes 360ms per loop
While
python2.4 -m timeit.py "x = range(10000)
while x:
    x.pop()
"
Takes only 30ms, and
python2.4 -m timeit "x=range(10000);
i=0
while i<len(x):
        x[i]
        i+=1
Takes 27.8
Interestingly enough:
$ python2.4 -m timeit "x=range(10000);
for y in x:
        y
"
Takes only 8.35ms.

So it seems that the 'for' construct really is the fastest. But you can
do even better, use a generator:
python2.4 -m timeit "for x in xrange(10000): x "
Takes only 5.41ms per loop.
And to prove that it really is doing something:
python2.4 -m timeit "for x in xrange(10000): pass"
takes only 3.61ms per loop.

And finally, you really do want 10000, because down at 1000 the
differences are pretty negligible. The indexed loop versus the front pop
has a difference of 2.5ms versus 3ms. Not huge. (The generator no-op
loop is down at 0.36ms, though)

This is definitely a tangent, but one I thought was fun to explore.
John
=:->
PS> You need python2.4 to support the '-m' flag (run a module in the
library without a full path)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 253 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050705/a925d4d5/attachment.pgp 


More information about the bazaar mailing list