New submission from Xiang Zhang: 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. ---------- components: Interpreter Core files: latin1-str-count-one-character.patch keywords: patch messages: 282784 nosy: haypo, serhiy.storchaka, xiang.zhang priority: normal severity: normal stage: patch review status: open title: Make str.count one character for latin1 string faster type: enhancement versions: Python 3.7 Added file: http://bugs.python.org/file45816/latin1-str-count-one-character.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue28921> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com