
Hi Tuom, On Mon, Sep 28, 2015 at 3:57 PM, Tuom Larsen <tuom.larsen@gmail.com> wrote:
from timeit import timeit print timeit('b = a[:]', 'a = list(range(100))') # 0.0732719898224 print timeit('b = list(a)', 'a = list(range(100))') # 0.00285792350769
In addition to the other comments, this is not testing what you think it is: 'range(100)' returns a list optimized for being a range, and 'list(range(100))' does it too. You can see it like this:
import __pypy__ __pypy__.strategy([]) 'EmptyListStrategy' __pypy__.strategy(range(100)) 'SimpleRangeListStrategy' __pypy__.strategy(list(range(100))) 'SimpleRangeListStrategy'
The SimpleRangeListStrategy means that the list is implemented as just a number, here 100.
__pypy__.strategy(range(100)[:]) 'IntegerListStrategy'
So we discover here that slicing a range-list is not as optimized as it could be. It expands it to an array containing all the integers. That's the reason for why the slicing version seems slower in your tests. But optimizing that would of course be pointless in almost any real program. A bientôt, Armin.