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 constructor speed for typical inputs by skirpichev · Pull Request #134320 · python/cpython Conversation
|
| Benchmark | ref | patch |
|---|---|---|
| Fraction(myint) | 5.62 us | 7.03 us: 1.25x slower |
| Fraction(Fraction(1, 2)) | 5.89 us | 4.88 us: 1.21x faster |
| Fraction(Decimal('0.25')) | 11.1 us | 7.98 us: 1.39x faster |
| Fraction(IntSubclass(123)) | 5.93 us | 5.05 us: 1.17x faster |
| Fraction(FloatSubclass(0.25)) | 7.69 us | 4.26 us: 1.81x faster |
| Fraction('123') | 19.2 us | 16.1 us: 1.19x faster |
| Fraction('1/3') | 19.6 us | 16.4 us: 1.20x faster |
| Fraction('1.2e-3') | 26.1 us | 23.0 us: 1.14x faster |
| Fraction('-.2') | 23.4 us | 20.2 us: 1.16x faster |
| Geometric mean | (ref) | 1.21x faster |
# bench.py import pyperf from fractions import Fraction as F from decimal import Decimal as D from numbers import Integral runner = pyperf.Runner() s = 'Fraction' class MyInt: numerator = 123 denominator = 1 def __int__(self): return 123 def __repr__(self): return "myint" class IntSubclass(int): def __repr__(self): return 'IntSubclass('+super().__repr__()+')' class FloatSubclass(float): def __repr__(self): return 'FloatSubclass('+super().__repr__()+')' Integral.register(MyInt) for v in [MyInt(), F(1, 2), D("0.25"), IntSubclass(123), FloatSubclass(0.25), "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.
This moves abc check for numbers.Rational - down.
|
There is a slight behaviour change here: for A minimal example: I think the change is acceptable and would probably accept the PR. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
>>> help(fractions.Fraction.as_integer_ratio)
Help on function as_integer_ratio in module fractions:
as_integer_ratio(self)
Return a pair of integers, whose ratio is equal to the original Fraction.
The ratio is in lowest terms and has a positive denominator. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM. 👍
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…pythonGH-134320) This moves abc check for numbers.Rational - down.
…pythonGH-134320) This moves abc check for numbers.Rational - down.
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.