classification
Title: performance of some list slice assignment margin cases can be improved
Type: performance Stage: patch review
Components: Interpreter Core Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, sir-sigurd
Priority: normal Keywords: patch

Created on 2018-07-18 18:26 by sir-sigurd, last changed 2018-07-19 05:26 by rhettinger.

Pull Requests
URL Status Linked Edit
PR 8333 open sir-sigurd, 2018-07-18 18:27
Messages (2)
msg321908 - (view) Author: Sergey Fedoseev (sir-sigurd) * Date: 2018-07-18 18:26
Script for benchmarks:

NAME=list-slice.json

python -m perf timeit --name "l[len(l):] = reversed(l)" -l 1 -s "l = [None] * 1000000" "l[len(l):] = reversed(l)" --append $NAME
python -m perf timeit --name "l[len(l):] = l" -l 1 -s "l = [None] * 1000000" "l[len(l):] = l" --append $NAME
python -m perf timeit --name "l[::-1] = l" -s "l = [None] * 1000000" "l[::-1] = l" --append $NAME
python -m perf timeit --name "l[:] = l" -s "l = [None] * 1000000" "l[:] = l" --append $NAME
python -m perf timeit --name "l[len(l)//2:] = l" -l 1 -s "l = [None] * 1000000" "l[len(l)//2:] = l"  --append $NAME
python -m perf timeit --name "l[:len(l)//2] = l" -l 1 -s "l = [None] * 1000000" "l[:len(l)//2] = l" --append $NAME
python -m perf timeit --name "l[len(l)//4:len(l)*3//4] = l" -l 1 -s "l = [None] * 1000000" "l[len(l)//4:len(l)*3//4] = l" --append $NAME

Results table:

+------------------------------+-------------------+------------------------------------+
| Benchmark                    | list-slice-master | list-slice                         |
+==============================+===================+====================================+
| l[len(l):] = reversed(l)     | 8.44 ms           | 5.18 ms: 1.63x faster (-39%)       |
+------------------------------+-------------------+------------------------------------+
| l[len(l):] = l               | 7.88 ms           | 3.37 ms: 2.34x faster (-57%)       |
+------------------------------+-------------------+------------------------------------+
| l[::-1] = l                  | 10.4 ms           | 582 us: 17.85x faster (-94%)       |
+------------------------------+-------------------+------------------------------------+
| l[:] = l                     | 10.6 ms           | 86.1 ns: 123128.46x faster (-100%) |
+------------------------------+-------------------+------------------------------------+
| l[len(l)//2:] = l            | 11.1 ms           | 2.08 ms: 5.33x faster (-81%)       |
+------------------------------+-------------------+------------------------------------+
| l[:len(l)//2] = l            | 11.5 ms           | 1.76 ms: 6.53x faster (-85%)       |
+------------------------------+-------------------+------------------------------------+
| l[len(l)//4:len(l)*3//4] = l | 11.3 ms           | 2.27 ms: 4.98x faster (-80%)       |
+------------------------------+-------------------+------------------------------------+
msg321924 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-07-19 05:26
I recommend against this one.  The cases covered are too exotic to care about and don't warrant the code churn.
History
Date User Action Args
2018-07-19 05:26:31rhettingersetnosy: + rhettinger
messages: + msg321924
2018-07-19 03:54:57sir-sigurdsettitle: performance of some list slice assignment can be improved -> performance of some list slice assignment margin cases can be improved
2018-07-18 18:27:31sir-sigurdsetkeywords: + patch
stage: patch review
pull_requests: + pull_request7870
2018-07-18 18:26:09sir-sigurdcreate