-
-
Notifications
You must be signed in to change notification settings - Fork 92
Closed
Labels
Description
In below lines the regex pattern is compiled for each line in a loop which is not needed since we can just use re.compile
once and use the pattern object. This improves the speed as per below benchmarks.
importlib_metadata/importlib_metadata/__init__.py
Lines 528 to 535 in 5c9198c
def _read_sections(lines): | |
section = None | |
for line in filter(None, lines): | |
section_match = re.match(r'\[(.*)\]$', line) | |
if section_match: | |
section = section_match.group(1) | |
continue | |
yield locals() |
Benchmark of comparing requires.txt for dask
$ wc requires.txt
38 31 427 requires.txt
$ python -m pyperf timeit -s 'from importlib_metadata import Distribution; requires = open("requires.txt").read()' 'Distribution._deps_from_requires_text(requires)' -o no_pattern.json
$ python -m pyperf timeit -s 'from importlib_metadata import Distribution; requires = open("requires.txt").read()' 'Distribution._deps_from_requires_text(requires)' -o pattern.json
$ python -m pyperf compare_to no_pattern.json pattern.json --table
+-----------+------------+-----------------------+
| Benchmark | no_pattern | pattern |
+===========+============+=======================+
| timeit | 53.4 us | 30.4 us: 1.76x faster |
+-----------+------------+-----------------------+
jaraco