On Nov 15, 2008, at 5:02 PM, pong wrote:

>
> According to diveintopython .extend should be faster than + since the
> former does not return a value.
> I was curious and tried
>
> %timeit [ ]+[2]
> 1000000 loops, best of 3: 741 ns per loop
> and
>
> %timeit [ ].extend([2])
> 1000000 loops, best of 3: 870 ns per loop
>
> so + actually beats .extend slightly.
>
> I got the same result when I tried that on sage online. (however
> %timeit only tried 625 loops instead of 10^7 loops, why?)
>
> Another puzzling thing is that
> b=[ ]
> %timeit b.extend([2])
> 1000000 loops, best of 3: 801 ns per loop
>
> type(b)
> <type 'list'>
>
> but then when I run
>
> %timeit b+[2]
> 10 loops, best of 3: 88.9 ms per loop
>
> That's much slower, in fact if you simple type b and hit enter... SAGE
> hangs.
>
> why?

It is a function of how long the list is. Calling a.extend(b) avoid  
copying the list, but requires an attribute lookup and python call.  
As the list gets longer, extend gets faster.

- Robert

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to