GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing github.com — gh-150942: Speed up re.findall and re.sub/subn result building by eendebakpt · Pull Request #150943 · python/cpython Conversation
|
| Benchmark | main | this PR | speedup |
|---|---|---|---|
re_sub |
1.93 ms | 1.73 ms | 1.11× |
re_subn |
1.92 ms | 1.73 ms | 1.11× |
re_findall |
1.89 ms | 1.80 ms | 1.05× |
re_findall_groups |
4.01 ms | 3.87 ms | 1.04× |
| geomean | 1.06× |
"""Microbenchmark re.findall / split / sub / subn (result-list building).""" import re import pyperf WORDS = "the quick brown fox jumps over the lazy dog " * 2000 CSVISH = "field1,field2,field3,field4,field5,field6,field7,field8\n" * 3000 word_re = re.compile(r"\w+") group_re = re.compile(r"(\w)(\w+)") split_re = re.compile(r"[,\n]") sub_re = re.compile(r"\w+") def b_findall(): return word_re.findall(WORDS) def b_findall_groups(): return group_re.findall(WORDS) # list of tuples def b_sub(): return sub_re.sub("X", WORDS) def b_subn(): return sub_re.subn("X", WORDS) if __name__ == "__main__": runner = pyperf.Runner() runner.bench_func("re_findall", b_findall) runner.bench_func("re_findall_groups", b_findall_groups) runner.bench_func("re_sub", b_sub) runner.bench_func("re_subn", b_subn)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Append result items to the output list with _PyList_AppendTakeRef instead of PyList_Append followed by Py_DECREF, removing a reference-count round-trip per appended item (and a per-append lock on the free-threaded build). Applied to the result lists built by findall and the sub/subn helper, where the append is a meaningful share of the work. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM. The code looks simpler, and it also brings a performance improvement.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.