-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Optimize textwrap.indent() #107369
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
Labels
Comments
import timeit
import textwrap
with open("Objects/unicodeobject.c") as f:
text = f.read()
it = timeit.Timer(lambda: textwrap.indent(text, " "*4))
result = it.repeat(number=1000)
result.sort()
print(f"indent {len(text.splitlines())} lines.")
print(f"{result[0]:.4f}msec") before: 2.8msec |
methane
added a commit
that referenced
this issue
Jul 29, 2023
AA-Turner
added a commit
that referenced
this issue
Mar 31, 2025
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Moved to #131919 (comment) |
Sorry for my confusion. |
seehwan
pushed a commit
to seehwan/cpython
that referenced
this issue
Apr 16, 2025
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Current code:
predicate = str.strip
is faster thandef predicate(line)
''.join(x)
converts input iterable to sequence. Using generator just makes overhead.prefix + line
is avoidable.Linked PRs
textwrap.indent()
#131923The text was updated successfully, but these errors were encountered: