Fast fractions reimplementation in Cython
OTHER License
Python's Fraction
data type is an excellent way to do exact calculations
with unlimited rational numbers and largely beats Decimal
in terms of
simplicity, accuracy and safety. Clearly not in terms of speed, though,
given the cdecimal accelerator in Python 3.3+.
quicktions
is an adaptation of the original fractions
module
(as included in CPython 3.13a3) that is compiled and optimised with
Cython <https://cython.org/>
_ into a fast, native extension module.
Compared to the standard library fractions
module of CPython,
computations in quicktions
are about
Compared to the fractions
module in CPython 3.10, instantiation of a
Fraction
in quicktions
is also
Fraction("123.456789")
)Fraction(123.456789)
)Fraction(123, 456)
)We provide a set of micro-benchmarks here:
https://github.com/scoder/quicktions/tree/master/benchmark
As of quicktions 1.12, the different number types and implementations compare as follows in CPython 3.10:
.. code-block::
Average times for all 'create' benchmarks:
float : 36.17 us (1.0x)
Decimal : 111.71 us (3.1x)
Fraction : 111.98 us (3.1x)
PyFraction : 398.80 us (11.0x)
Average times for all 'compute' benchmarks:
float : 4.53 us (1.0x)
Decimal : 16.62 us (3.7x)
Fraction : 72.91 us (16.1x)
PyFraction : 251.93 us (55.6x)
While not as fast as the C implemented decimal
module in Python 3,
quicktions
is about 15x faster than the Python implemented decimal
module in Python 2.7.
For documentation, see the Python standard library's fractions
module: