Skip to content

gh-125196: Use PyUnicodeWriter in _io.StringIO - #125528

Closed
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:writer_stringio
Closed

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:writer_stringio

Conversation

@vstinner

@vstinner vstinner commented Oct 15, 2024 •

Copy link
Copy Markdown
Member
@vstinner

Copy link
Copy Markdown
Member Author

Benchmark:

import _io

def hello_world():
    s = _io.StringIO()
    s.write('Hello ')
    s.write('World!')
    s.getvalue()

def long_strings():
    s = _io.StringIO()
    s.write('a' * 10)
    s.write('a' * 100)
    s.write('a' * 1000)
    s.getvalue()

import pyperf
runner = pyperf.Runner()
runner.bench_func('hello_world', hello_world)
runner.bench_func('long_strings', long_strings)

Results, Python built with gcc -O3, CPU isolation:

+----------------+--------+----------------------+
| Benchmark      | ref    | writer               |
+================+========+======================+
| hello_world    | 322 ns | 352 ns: 1.09x slower |
+----------------+--------+----------------------+
| long_strings   | 511 ns | 547 ns: 1.07x slower |
+----------------+--------+----------------------+
| Geometric mean | (ref)  | 1.08x slower         |
+----------------+--------+----------------------+

It makes StringIO slower :-(

@vstinner

Copy link
Copy Markdown
Member Author

It's slower so I don't think that it's worth it.

@vstinner vstinner closed this Oct 25, 2024
@vstinner
vstinner deleted the writer_stringio branch October 25, 2024 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant