Message362986
Using a list's insert function is much slower than using slice assignment:
> python -m timeit -n 100000 -s "a=[]" "a.insert(0,0)"
100000 loops, best of 5: 19.2 usec per loop
> python -m timeit -n 100000 -s "a=[]" "a[0:0]=[0]"
100000 loops, best of 5: 6.78 usec per loop
(Note that the list starts empty but grows to 100,000 elements.)
At first I thought maybe it's the attribute lookup or function call overhead or so, but inserting near the end shows that that's negligible:
> python -m timeit -n 100000 -s "a=[]" "a.insert(-1,0)"
100000 loops, best of 5: 79.1 nsec per loop
I asked at StackOverflow and someone pointed out that list.insert uses a manual loop instead of memmove: https://stackoverflow.com/a/60466572/12671057 |
|
Date |
User |
Action |
Args |
2020-02-29 16:11:28 | Stefan Pochmann | set | recipients:
+ Stefan Pochmann |
2020-02-29 16:11:28 | Stefan Pochmann | set | messageid: <1582992688.24.0.071544861703.issue39801@roundup.psfhosted.org> |
2020-02-29 16:11:28 | Stefan Pochmann | link | issue39801 messages |
2020-02-29 16:11:27 | Stefan Pochmann | create | |
|