Skip to content

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

Open
skirpichev wants to merge 13 commits into
python:mainfrom
skirpichev:speedup-Fraction-fromstr/72902
Open

gh-72902: improve Fraction(str) speed (don't use regexp's)#133994
skirpichev wants to merge 13 commits into
python:mainfrom
skirpichev:speedup-Fraction-fromstr/72902

Conversation

@skirpichev

@skirpichevskirpichev commented May 14, 2025

Copy link
Copy Markdown
Member

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
Details
# bench.pyimportpyperffromfractionsimportFractionasFrunner=pyperf.Runner()
s='Fraction'forvin ["123", "1/3", "1.2e-3", "-.2"]:
r=s+'('+repr(v) +')'runner.bench_func(r, F, v)

@skirpichev
skirpichev marked this pull request as ready for review May 14, 2025 11:01
Comment threadLib/fractions.py Outdated
@eendebakpt

This comment was marked as resolved.

Comment threadLib/fractions.py
serhiy-storchaka

This comment was marked as resolved.

@serhiy-storchaka

This comment was marked as resolved.

@skirpichev

This comment was marked as resolved.

@skirpichev
skirpichev marked this pull request as draft May 14, 2025 15:10
skirpichevand others added 3 commits May 16, 2025 06:37
Co-authored-by: Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
@skirpichev
skirpichevforce-pushed the speedup-Fraction-fromstr/72902 branch from 3687e34 to 05e9110CompareMay 16, 2025 03:37
@skirpichev
skirpichev marked this pull request as ready for review May 16, 2025 03:53
@skirpichev

This comment was marked as resolved.

Comment threadLib/fractions.py Outdated
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>

@eendebakpteendebakpt left a comment

Copy link
Copy Markdown
Contributor

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 for Fraction('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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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) and my arguments for this.

@skirpichev
skirpichev marked this pull request as draft May 20, 2025 07:56
@skirpichev

Copy link
Copy Markdown
MemberAuthor

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

@skirpichev
skirpichev marked this pull request as ready for review May 20, 2025 09:13
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsBot added the stale Stale PR or inactive for long period of time. label Apr 24, 2026
@skirpichev
skirpichev deleted the speedup-Fraction-fromstr/72902 branch April 24, 2026 23:03
@skirpichev
skirpichev restored the speedup-Fraction-fromstr/72902 branch May 23, 2026 07:42
@skirpichevskirpichev reopened this May 23, 2026
@github-actionsgithub-actionsBot removed the stale Stale PR or inactive for long period of time. label May 23, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@skirpichev@eendebakpt@serhiy-storchaka@PPvX-fulump@Ritiksh19