
Hello Armin, Thanks for the answer! On Mon, Sep 28, 2015 at 8:17 PM, Armin Rigo <arigo@tunes.org> wrote:
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.
However, I still get "slice-way" twice as slow as "constructor-way" when I use normal lists: from timeit import timeit print timeit('b = a[:]', 'a = [1,2,3,4,5,6,7,8,9,0]') # 0.0329349040985 print timeit('b = list(a)', 'a = [1,2,3,4,5,6,7,8,9,0]') # 0.0171940326691 or: pypy -m timeit -s 'a = [1,2,3,4,5,6,7,8,9,0]' 'b = a[:]' # 0.0306 usec per loop pypy -m timeit -s 'a = [1,2,3,4,5,6,7,8,9,0]' 'b = list(a)' # 0.0149 usec per loop Please, let me rephrase my question: currently I use `[:]` because it is faster in CPython (0.131 usec vs 0.269 usec per loop). I almost don't mind changing it to `list()` because of PyPy but I was wondering what do PyPy developers recommend. I don't understand why is `[:]` twice as slow as `list()` as it seems it should do the same thing (create a list and copy the content). So my question is, is there a reason why `list()` is fundamentally faster, and then I would switch to it, or maybe/perhaps/somewhere-in-the-future PyPy would make it as fast as `[:]`, as it does the same amount of work, and then I could leave it as it is (`[:]`)?