Skip to content

bpo-32677: Optimize str.isascii()#5356

Merged
methane merged 2 commits intopython:masterfrom
methane:optimize-str-isascii
Jan 28, 2018
Merged

bpo-32677: Optimize str.isascii()#5356
methane merged 2 commits intopython:masterfrom
methane:optimize-str-isascii

Conversation

@methane
Copy link
Copy Markdown
Member

@methane methane commented Jan 27, 2018

@vstinner
Copy link
Copy Markdown
Member

Can you try a microbenchmark of bytes object of 256 bytes and 1 MB?

@methane
Copy link
Copy Markdown
Member Author

methane commented Jan 27, 2018

$ ./python -m perf timeit --compare-to=`pwd`/python-master -s 'b=b"x"*(1024**2)' -- 'b.isascii()'
python-master: ..................... 679 us +- 0 us
python: ..................... 84.9 us +- 0.0 us

Mean +- std dev: [python-master] 679 us +- 0 us -> [python] 84.9 us +- 0.0 us: 7.99x faster (-87%)

$ ./python -m perf timeit --compare-to=`pwd`/python-master -s 'b=b"x"*256' -- 'b.isascii()'
python-master: ..................... 228 ns +- 1 ns
python: ..................... 76.8 ns +- 1.8 ns

Mean +- std dev: [python-master] 228 ns +- 1 ns -> [python] 76.8 ns +- 1.8 ns: 2.97x faster (-66%)

$ ./python -m perf timeit --compare-to=`pwd`/python-master -s 'b=b"x"*39' -- 'b.isascii()'
python-master: ..................... 81.5 ns +- 2.1 ns
python: ..................... 64.3 ns +- 1.1 ns

Mean +- std dev: [python-master] 81.5 ns +- 2.1 ns -> [python] 64.3 ns +- 1.1 ns: 1.27x faster (-21%)

@methane methane changed the title [wip] bpo-32677: Optimize str.isascii() bpo-32677: Optimize str.isascii() Jan 27, 2018
Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Impressive benchmark, it's really 8x faster for long strings. 20 ns faster on 80 ns for short strings is also nice to have!

@methane methane merged commit bea5706 into python:master Jan 28, 2018
@methane methane deleted the optimize-str-isascii branch January 28, 2018 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants