Repository navigation
Conversation
395c279 to
002be51
Compare
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
002be51 to
abfa80d
Compare
|
(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
|
How about concatenating everything and making a single |
|
I started with that but then found it didn't produce uniform speedup: Copying strings of various sizes into a PyUnicodeWriter instead of doing the "pending" list that TextIOWrapper does would slowdown for some string sizes. It also very significantly changes behavior if a In the longer term I'm hoping to add what I call a "Coherent" print; everything in one print statement gets |
file.writeonce perprint()call, instead of once for each argument, separator and line ending.' 'and'\n'strings for the defaultsepandenddirectly, instead of converting them from C strings withPyFile_WriteString(), which runsstrlen()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.
print()by looking upfile.writeonce per call #160725