Skip to content

Optimize textwrap.indent a bit more. #107424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
picnixz opened this issue Jul 29, 2023 · 1 comment
Closed

Optimize textwrap.indent a bit more. #107424

picnixz opened this issue Jul 29, 2023 · 1 comment
Labels
3.13 bugs and security fixes performance Performance or resource usage stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@picnixz
Copy link
Member

picnixz commented Jul 29, 2023

The optimization introduced in #107374 can be improved as follows:

Current code

if predicate is None:
    predicate = lambda s: not s.isspace()

    prefixed_lines = []
    for line in text.splitlines(True):
        if predicate(line):
            prefixed_lines.append(prefix)
        prefixed_lines.append(line)

Improved code

prefixed_lines = []

if predicate is None:
    for line in text.splitlines(True):
        if not line.isspace():
            prefixed_lines.append(prefix)
        prefixed_lines.append(line)
else:
    for line in text.splitlines(True):
        if predicate(line):
            prefixed_lines.append(prefix)
        prefixed_lines.append(line)

Benchmarks

import timeit
import textwrap

with open("Objects/unicodeobject.c") as f:
    text = f.read()

print(f"indent {len(text.splitlines())} lines.")

it = timeit.Timer(lambda: textwrap.indent(text, ' ' * 4))
result = it.repeat(number=1000)
result.sort()
print(f"{result[0]:.4f}msec")
  • Current: 7.9305msec
  • After: 6.7143msec

However the results are very different between two runs (and my laptop may be dying) so I'd like some confirmation from others.

Linked PRs

@picnixz picnixz added the type-feature A feature request or enhancement label Jul 29, 2023
@hugovk
Copy link
Member

hugovk commented Jul 29, 2023

cc @methane

@AlexWaygood AlexWaygood added performance Performance or resource usage stdlib Python modules in the Lib dir 3.13 bugs and security fixes labels Jul 29, 2023
picnixz added a commit to picnixz/cpython that referenced this issue Jul 29, 2023
@methane methane closed this as not planned Won't fix, can't repro, duplicate, stale Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes performance Performance or resource usage stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants