Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Shouldn't you omit `-s` in the last one? I think you're just effectively testing an empty loop, since you're only actually creating the list in the setup phase.

On my machine, the tests that actually do work are:

    $ python3 -m timeit -s 't = (1, 2, "a")' 'list(t)'
    5000000 loops, best of 5: 44.8 nsec per loop
    $ python3 -m timeit -s 't = (1, 2, "a")' '[*t]'
    10000000 loops, best of 5: 22.9 nsec per loop
    $ python3 -m timeit '[1, 2, "a"]'
    10000000 loops, best of 5: 23.1 nsec per loop
...compared to the empty ones:

    $ python3 -m timeit -s '[1, 2, "a"]'
    50000000 loops, best of 5: 5.43 nsec per loop
    $ python3 -m timeit ''
    50000000 loops, best of 5: 5.8 nsec per loop
(11th gen Intel i7 laptop, Python 3.10.6)


You’re completely right, I missed stripping it, my bad (and from the disassembly below we can see that the second and third do about the same operation so they should have very similar if not identical performances, I should have caught that).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: