Message282784
I make a try to improve str.count one character for latin1 string. From benchmark, it could enhance the speed depending on the length of the string.
# short one, a little regression
./python3 -m perf timeit --compare-to ~/cpython/python -s 's="abcdefg"' 's.count("a")'
python: ..................... 190 ns +- 1 ns
python3: ..................... 197 ns +- 2 ns
Median +- std dev: [python] 190 ns +- 1 ns -> [python3] 197 ns +- 2 ns: 1.04x slower
# a little longer
./python3 -m perf timeit --compare-to ~/cpython/python -s 's="abcdefghi"' 's.count("a")'
python: ..................... 192 ns +- 15 ns
python3: ..................... 184 ns +- 2 ns
Median +- std dev: [python] 192 ns +- 15 ns -> [python3] 184 ns +- 2 ns: 1.05x faster
# longer
./python3 -m perf timeit --compare-to ~/cpython/python -s 's="abcdefghihijklmnopqrstuvwxyz~!@##$%^&*()-=_+{}|"' 's.count("a")'
python: ..................... 236 ns +- 12 ns
python3: ..................... 209 ns +- 9 ns
Median +- std dev: [python] 236 ns +- 12 ns -> [python3] 209 ns +- 9 ns: 1.13x faster
# very long
./python3 -m perf timeit --compare-to ~/cpython/python -s 's="abcdefgHijk"*100' 's.count("z")'
python: ..................... 1.02 us +- 0.00 us
python3: ..................... 610 ns +- 1 ns
Median +- std dev: [python] 1.02 us +- 0.00 us -> [python3] 610 ns +- 1 ns: 1.67x faster
And since str.replace may also go through the code path involving count, it's somewhat affected:
./python3 -m perf timeit --compare-to ~/cpython/python -s 's="abcdefghihijklmnopqrstuvwxyz~!@##$%^&*()-=_+{}|"' 's.replace("a", "ccc")'
python: ..................... 318 ns +- 31 ns
python3: ..................... 298 ns +- 19 ns
Median +- std dev: [python] 318 ns +- 31 ns -> [python3] 298 ns +- 19 ns: 1.07x faster
For ucs2 and ucs4, the benchmarks are not appealing so leave them untouched. |
|
Date |
User |
Action |
Args |
2016-12-09 14:38:24 | xiang.zhang | set | recipients:
+ xiang.zhang, vstinner, serhiy.storchaka |
2016-12-09 14:38:24 | xiang.zhang | set | messageid: <1481294304.37.0.437308839314.issue28921@psf.upfronthosting.co.za> |
2016-12-09 14:38:24 | xiang.zhang | link | issue28921 messages |
2016-12-09 14:38:23 | xiang.zhang | create | |
|