GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing github.com — gh-72902: improve Fraction(str) speed (don't use regexp's) by skirpichev · Pull Request #133994 · python/cpython Conversation
|
| Benchmark | ref | patch |
|---|---|---|
| Fraction('123') | 19.4 us | 15.3 us: 1.27x faster |
| Fraction('1/3') | 19.7 us | 12.6 us: 1.56x faster |
| Fraction('1.2e-3') | 26.5 us | 19.9 us: 1.33x faster |
| Fraction('-.2') | 23.6 us | 18.1 us: 1.30x faster |
| Geometric mean | (ref) | 1.36x faster |
# bench.py import pyperf from fractions import Fraction as F runner = pyperf.Runner() s = 'Fraction' for v in ["123", "1/3", "1.2e-3", "-.2"]: r = s + '(' + repr(v) + ')' runner.bench_func(r, F, v)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Co-authored-by: Wolfgang Maier <[email protected]>
Co-authored-by: Pieter Eendebak <[email protected]>
3687e34 to
05e9110
Compare
There was an error while loading. Please reload this page.
Co-authored-by: Pieter Eendebak <[email protected]>
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Done.
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? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
But not too much.
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ok, now this has only optimization for str inputs. Other goes to #134320 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 30 days with no activity. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Documentation build overview
482 files changed ·
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.