Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-72902: improve Fraction(str) speed (don't use regexp's)#133994

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
skirpichev wants to merge9 commits intopython:main
base:main
Choose a base branch
Loading
fromskirpichev:speedup-Fraction-fromstr/72902

Conversation

skirpichev
Copy link
Member

@skirpichevskirpichev commentedMay 14, 2025
edited
Loading


For reviewers. I'm not sure if it worth (code seems more complex, IMO), but speedup for string parsing seems noticeable (1.3-1.5x).

Benchmarkrefpatch
Fraction('123')19.4 us15.3 us: 1.27x faster
Fraction('1/3')19.7 us12.6 us: 1.56x faster
Fraction('1.2e-3')26.5 us19.9 us: 1.33x faster
Fraction('-.2')23.6 us18.1 us: 1.30x faster
Geometric mean(ref)1.36x faster
# bench.pyimportpyperffromfractionsimportFractionasFrunner=pyperf.Runner()s='Fraction'forvin ["123","1/3","1.2e-3","-.2"]:r=s+'('+repr(v)+')'runner.bench_func(r,F,v)

@skirpichevskirpichev marked this pull request as ready for reviewMay 14, 2025 11:01
@eendebakpt

This comment was marked as resolved.

serhiy-storchaka

This comment was marked as resolved.

@serhiy-storchaka

This comment was marked as resolved.

@skirpichev

This comment was marked as resolved.

@skirpichevskirpichev marked this pull request as draftMay 14, 2025 15:10
skirpichevand others added3 commitsMay 16, 2025 06:37
Co-authored-by: Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
@skirpichevskirpichevforce-pushed thespeedup-Fraction-fromstr/72902 branch from3687e34 to05e9110CompareMay 16, 2025 03:37
@skirpichevskirpichev marked this pull request as ready for reviewMay 16, 2025 03:53
@skirpichev

This comment was marked as resolved.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Copy link
Contributor

@eendebakpteendebakpt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@skirpichev Could you also add a test case forFraction('232e\t2') (this should fail). One optimization I tried passes all the tests, but fails on this one.

Tests pass, and all paths in the new code are needed, so approving. The new code is not very easy to read, but neither is the regular expression that is currently in main.

@skirpichev
Copy link
MemberAuthor

Could you also add a test case for Fraction('232e\t2') (this should fail).

Done.

The new code is not very easy to read, but neither is the regular expression that is currently in main.

I think that the later is more clear. Also, it has more nice tracebacks.

So, I'm still not sure about this. Speed gain is x2 at most. Maybe regexps can be eventually optimized?

@serhiy-storchaka
Copy link
Member

I was surprised that manual parsing is faster than a regexp-based approach. The regular expression is not optimal, so I tried to optimize it, but the benefit was minuscule. The regexp engine needs to be optimized.

Can we please wait a few months with these changes while I try to optimize the regexp engine for such case? This seems so typical that a lot of code would benefit from optimization.

@skirpichev
Copy link
MemberAuthor

I was surprised that manual parsing is faster than a regexp-based approach.

But not too much.

The regexp engine needs to be optimized.
Can we please wait a few months with these changes while I try to optimize the regexp engine for such case?

Sure. I'm not familiar with the regexp module code and I would be interested in such fix, if it's possible at all. BTW, so far this pr was not approved by core dev.

@serhiy-storchaka, but what about other change, i.e. moving down numbers.Rational check? Maybe I should factor out this to a separate pr? See benchmarks (third column) andmy arguments for this.

@skirpichevskirpichev marked this pull request as draftMay 20, 2025 07:56
@skirpichev
Copy link
MemberAuthor

Ok, now this has only optimization for str inputs. Other goes to#134320

@skirpichevskirpichev marked this pull request as ready for reviewMay 20, 2025 09:13
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@eendebakpteendebakpteendebakpt approved these changes

@serhiy-storchakaserhiy-storchakaAwaiting requested review from serhiy-storchaka

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@skirpichev@eendebakpt@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp