Skip to content

gh-160725: Speed up print() by looking up write() once per call - #160727

Open
cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:print_lookup_once
Open

cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:print_lookup_once

Conversation

@cmaloney

@cmaloney cmaloney commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor
  • Look up file.write once per print() call, instead of once for each argument, separator and line ending.
  • Pass the cached ' ' and '\n' strings for the default sep and end directly, instead of converting them from C strings with PyFile_WriteString(), which runs strlen() and the UTF-8 decoder on every call to get back the same cached string.

write() receives the same calls as before. Benchmarks are in the issue: 1.20x faster geometric mean across 8 cases, 5 faster (1.12x to 1.66x) and 3 not significant.

Developed with the help of AI: Claude Opus 5.5.

@python-cla-bot

python-cla-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Look up the file's write() method once per print() call, instead of once
for each argument, separator and line ending, and pass the cached ' ' and
'\n' strings for the default sep and end directly, instead of converting
them from C strings with PyFile_WriteString().  write() is called exactly
as before.

print() is up to 1.66x faster in pyperf benchmarks, 1.20x geometric
mean; for example, print('a', 1, 2.5) to a file goes from 996 ns to
687 ns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129ST3GmU89AvSnUNSXu2Yi
@cmaloney

Copy link
Copy Markdown
Contributor Author

(force pushed to fixup author line and make cla bot happy)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129ST3GmU89AvSnUNSXu2Yi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant