python 中更快的分数模块
是否有更快的 Fractions 模块,例如 cFractions 模块,就像有一个更快的 cDecimal 模块一样Decimal
模块的? fractions
模块太慢。
Is there a faster equivalent of the fractions
module, something like a cFractions
module, just as there is a cDecimal
module, which is a faster equivalent of the Decimal
module ? The fractions
module is too slow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也因缺少这个包而苦苦挣扎,并决定实现一个名为
cfractions
< /a> (源代码Github 上)。我们唯一需要的就是安装它
,然后在模块中将
fractions
替换为cfractions
,就这么简单。主要特点包括
内存更少
<前><代码>>>>从 cffractions 导入分数
>>>>>导入系统
>>>>> sys.getsizeof(分数())
32
相比
<前><代码>>>>从分数导入分数
>>>>>导入系统
>>>>> sys.getsizeof(分数())
48
所以它基本上是一个简单的 Python
object
+2
分子和指针。分母。更快的速度:
int
构造浮点构建
str 构建
n
个实例的总和n
个实例的乘积或者如果我们看一下相对表现
我们可以看到
fractions.Fraction
飙升,耶!注意:我正在使用
perfplot
包< /a>,所有基准测试都在Python3.9.4
上运行。Python3.5+
支持,纯 Python C API,无额外依赖项,
由分子/分母对构造,单个
int
/float
/任何numbers.Rational
值,str
(来自 版本1.4.0
),充满恐惧算术&比较操作,
字符串表示(
__repr__
和__str__
),pickle
ing 和copy
ing,不变性和
复制
ing可散列性,使用
int
和float
进行操作(对于后者,将Fraction
实例转换为float
,就像fractions.Fraction
一样),PyPy
支持(通过回退到fractions.Fraction
代理),使用
假设
框架。它不包括
I was struggling with lack of this package as well and decided to implement one called
cfractions
(source code available on Github).The only thing we need is to install it
and after that replace
fractions
withcfractions
in your modules, as easy as that.Main features include
less memory
compared to
so it's basically a plain Python
object
+2
pointers for numerator & denominator.more speed:
int
float
str
n
instancesn
instancesor if we take a look at relative performance
we can see
fractions.Fraction
skyrocketing, yay!Note: I'm using
perfplot
package, all benchmarks run onPython3.9.4
.Python3.5+
support,plain Python C API, no additional dependencies,
constructing from numerator/denominator pair, single
int
/float
/anynumbers.Rational
value,str
(from version1.4.0
),full spectre of arithmetic & comparison operations,
string representation (both
__repr__
&__str__
),pickle
ing andcopy
ing,immmutability & hashability,
operating with
int
andfloat
(with conversion ofFraction
instance tofloat
for the latter, as it is forfractions.Fraction
),PyPy
support (by falling back tofractions.Fraction
proxy),property-based tests of all operations using
Hypothesis
framework.What it doesn't include
complex
.使用 http://code.google.com/p/gmpy/
它使用 GMP 多重-用于快速整数和有理算术的精度库。
注意:我也是维护者。
Use http://code.google.com/p/gmpy/
It uses the GMP mutliple-precision library for fast integer and rational arithmetic.
Note: I'm also the maintainer.
不幸的是,如果不需要编译的外部依赖项,就没有可用的 c 等效项。根据您的需求,我提出的要点: https://gist.github.com/mscuthbert/f22942537ebbba2c31d4 可能会有所帮助。
它公开了一个函数
opFrac(num)
,该函数可以选择将 int、float 或 Fraction 转换为具有分母限制的 float 或 Fraction(我使用 65535,因为我正在处理小分数);如果浮点数可以精确地用二进制表示(即,它是两个分母的某个幂的倍数),那么就不用管它了。否则它将其转换为分数。类似地,如果分数可以用二进制精确表示,我们将其转换为浮点数;否则我们就不管它了。Fraction(float).limit_denominator(x)
调用被提取到辅助函数_preFracLimitDenominator
中,该函数仅创建一个Fraction
对象,而不是通常通过调用创建的三个。这一要点的用例很少,但只要存在,结果就会非常惊人。对于我的项目 music21,我们主要使用通常放置在节拍(整数)或二分之一、四分之一、八分之一等节拍(完全可以用二进制表示)上的音符,但在极少数情况下,音符有放置(偏移)或持续时间,即节拍的 1/3 或 1/5,我们遇到了大的浮点转换问题,导致了模糊的错误。我们的测试套件使用浮点偏移和持续时间运行了 350 秒。将所有内容都切换为分数会使时间增加到 1100 秒——完全不可接受。切换到可选的分数并快速创建分数将时间缩短至 360 秒,或者说性能仅下降了 3%。
如果您有时可以处理浮点数,有时可以处理分数,那么这可能是正确的选择。
Unfortunately, there's no c equivalent available without needing a compiled external dependency. Depending on your needs, the gist I've made: https://gist.github.com/mscuthbert/f22942537ebbba2c31d4 may help.
It exposes a function
opFrac(num)
that optionally converts an int, float, or Fraction into a float or Fraction with a denominator limit (I use 65535 because I'm working with small fractions); if the float can be exactly represented in binary (i.e., it's a multiple of some power of two denominator), it leaves it alone. Otherwise it converts it to a Fraction. Similarly, if the Fraction is exactly representable in binary we convert it to a float; otherwise we leave it alone.The
Fraction(float).limit_denominator(x)
call is extracted out into a helper function,_preFracLimitDenominator
, that only creates oneFraction
object rather than the three normally created with the call.The use cases for this gist are pretty few, but where they exist, the results are spectacular. For my project, music21, we work mostly with notes that are generally placed on a beat (integer) or on a half, quarter, eighth, etc. beat (exactly representable in binary), but on the rarer occasions when notes have placement (offset) or duration that is, say, 1/3 or 1/5 of a beat, we were running into big floating point conversion problems that led to obscure bugs. Our test suite was running in 350 seconds using floating point offsets and durations. Switching everything to Fractions ballooned the time to 1100 seconds -- totally unacceptable. Switching to optional Fractions with fast Fraction creation brought the time back to 360 seconds, or only a 3% performance hit.
If you can deal with sometimes working with floats and sometimes Fractions, this may be the way to go.
我什么也没找到。
你可以制作一个。http://docs.python.org/extending/extending.html
快速搜索 c 中的分数给了我 http://www.spiration.co.uk/post/1400/fractions-in-c---a-rational-arithmetic-library。使用第二篇文章,它也处理负数。
但这可能不是您需要的,您可以找到其他东西。如果你不想扩展 python,并且找不到任何拥有 cFractions 模块的人,则必须坚持使用 Fractions。对不起。
I couldn't find anything.
You could make one.http://docs.python.org/extending/extending.html
A quick search on fractions in c gave me http://www.spiration.co.uk/post/1400/fractions-in-c---a-rational-arithmetic-library. Use the 2nd post, it also handles negative numbers.
But that may not be what you need and you can find something else. If you don't want to extend python you have to stick to Fractions if you can't find anyone who has a cFractions module. I'm sorry.