On Sat, 15 May 2021 at 12:52, Steven D'Aprano <st...@pearwood.info> wrote:
>
> On Fri, May 14, 2021 at 09:39:33PM +0100, Oscar Benjamin wrote:
>
> > Yes, but having a faster fraction type would be great. SymPy doesn't
> > actually use the fractions module because it's too slow. Instead SymPy
> > has its own pure Python implementation that is a little faster and
> > will use gmpy2's mpq type if gmpy2 is installed. The mpq type is
> > something like 30x faster than Fraction. The fmpq type from
> > python_flint is around twice as fast again and is particularly fast
> > for small rationals.
>
> Can we not improve the implementation of Fraction? What do the non-gmpy
> versions do that makes them faster?
>
> The statistics module relies on Fraction for much of its calculations,
> improving Fraction would have a direct benefit for that. Just sayin'...

SymPy's fallback PythonMPQ class is here:
https://github.com/sympy/sympy/blob/e30fcee96c3dbec337bc665229e5ef685277d56e/sympy/external/pythonmpq.py#L9-L16
It isn't intended to be a public API so for example it doesn't enforce
immutability. That makes accessing numerator and denominator more
efficient than having a property method but this is probably not that
significant. Care is taken to structure operations so that it can
avoid unnecessary gcd calls but that's true of Fraction as well.

The main difference when I checked seemed to be the arithmetic
algorithms but looking now I can see that Fraction was recently
changed to use the same algorithms as SymPy:
https://github.com/python/cpython/commit/690aca781152a498f5117682524d2cd9aa4d7657
It looks like Sergey saw my comments when refactoring the PythonMPQ
code and upstreamed the algorithms.

Maybe it will now be possible to eliminate PythonMPQ which is
redundant with Fraction if Fraction is made faster (once support for
Python 3.9 is dropped).


Oscar
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UV4GVY4MMI6CNZDS3BBR27QZICPUJZZC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to