Repository navigation
Commit 1955eb3
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.
Rebinding file.write during print() keeps working as before. GH-160803 significantly lowered binding cost. Could save ~5ns/call on x64 linux by binding only once, but that is left for work in https://discuss.python.org/t/make-print-use-a-single-write-call/109886
| Benchmark | main | this PR |
|------------------------------------------------|:-------:|:---------------------:|
| print(file=devnull) | 51.2 ns | 47.4 ns: 1.08x faster |
| print('hello world', file=devnull) | 81.0 ns | 79.2 ns: 1.02x faster |
| print('a', 1, 2.5, file=devnull) | 281 ns | 278 ns: 1.01x faster |
| print('a', 'b', sep='-', end='', file=devnull) | 124 ns | 126 ns: 1.02x slower |
Benchmark hidden because not significant (1): print(*ten_ints, file=devnull)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f767423 commit 1955eb3
1 file changed
Lines changed: 8 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2240 | 2240 | | |
2241 | 2241 | | |
2242 | 2242 | | |
2243 | | - | |
| 2243 | + | |
2244 | 2244 | | |
2245 | 2245 | | |
2246 | 2246 | | |
| |||
2259 | 2259 | | |
2260 | 2260 | | |
2261 | 2261 | | |
2262 | | - | |
| 2262 | + | |
2263 | 2263 | | |
2264 | 2264 | | |
2265 | 2265 | | |
| |||
2269 | 2269 | | |
2270 | 2270 | | |
2271 | 2271 | | |
2272 | | - | |
| 2272 | + | |
2273 | 2273 | | |
2274 | 2274 | | |
2275 | 2275 | | |
| |||
2280 | 2280 | | |
2281 | 2281 | | |
2282 | 2282 | | |
2283 | | - | |
2284 | | - | |
2285 | | - | |
2286 | | - | |
2287 | | - | |
2288 | | - | |
2289 | | - | |
2290 | | - | |
2291 | | - | |
2292 | | - | |
2293 | | - | |
| 2283 | + | |
| 2284 | + | |
| 2285 | + | |
2294 | 2286 | | |
2295 | | - | |
2296 | | - | |
| 2287 | + | |
2297 | 2288 | | |
2298 | 2289 | | |
2299 | 2290 | | |
2300 | 2291 | | |
2301 | | - | |
2302 | | - | |
2303 | | - | |
2304 | | - | |
2305 | | - | |
2306 | | - | |
2307 | | - | |
2308 | | - | |
| 2292 | + | |
2309 | 2293 | | |
2310 | 2294 | | |
2311 | 2295 | | |
| |||
0 commit comments