[issue28921] Make str.count one character for latin1 string faster

Xiang Zhang report at bugs.python.org
Fri Dec 9 14:33:28 EST 2016


Xiang Zhang added the comment:

> I understand that count() is only used when the old and new patterns of str.replace() have a different length.

Yes. I thought it won't help much since str.replace get many operations. But for long string, looks good:

./python3 -m perf timeit --compare-to ~/cpython/python -s 's="abcdefghihijklmnopqrstuvwxyz~!@##$%^&*()-=_+{}|"*100' 's.replace("a", "bc")'
python: ..................... 7.36 us +- 0.04 us
python3: ..................... 4.91 us +- 0.04 us

Median +- std dev: [python] 7.36 us +- 0.04 us -> [python3] 4.91 us +- 0.04 us: 1.50x faster  # 50% ??!! how?

And this patch also applies to bytes since they share codes.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28921>
_______________________________________


More information about the Python-bugs-list mailing list