-
Notifications
You must be signed in to change notification settings - Fork 278
perf: better string building #997
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
Conversation
267b5d9 to
e584ec3
Compare
|
Almost all the savings here was from avoiding the NamedTuple indirection. This is now only 1% faster total time, probably 5% or something like that faster for the str operation. Saving the intermediate value doesn't have any measurable effect anymore, so I've remove that. Now it's mostly up to if you think this looks better (and it still is a little faster). |
I'm indifferent. |
|
I was also under the impression, apparently incorrectly, that join would be faster. Because naively concatenating strings can be O(n^2) with regards to memory allocation operations, and I thought the |
|
I'm nearly sure it's the fact the strings are generally small. If they were large I'm almost sure it would be the other way around. I played around with several ways to do this - I thought making all four separately then using an f-string to join them would be fastest, but short circuiting if None was too important. Now that the largest cost (accessing the nested field in the NamedTuple) is gone, it's possible that is faster. |
There's an optimization in CPython specifically for |
624a237 to
4a4953d
Compare
|
Okay, after one more change (inlining base_version), now this is much faster, around 10%. All benchmarks:
|
4a4953d to
4ebd365
Compare
Signed-off-by: Henry Schreiner <henryfs@princeton.edu> chore: remove saving intermediate (no longer much faster) Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
This makes the
__str__method faster, about 10%. This is used quite a bit, so saving some time here is useful.