有没有比 python 的 Decimal 更快的替代品?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/195116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a faster alternative to python's Decimal?
提问by Kozyarchuk
Does anyone know of a faster decimal implementation in python?
有谁知道python中更快的十进制实现?
As the example below demonstrates, the standard library's decimal module is ~100 times slower than float
.
如下例所示,标准库的十进制模块比float
.
from timeit import Timer
def run(val, the_class):
test = the_class(1)
for c in xrange(10000):
d = the_class(val)
d + test
d - test
d * test
d / test
d ** test
str(d)
abs(d)
if __name__ == "__main__":
a = Timer("run(123.345, float)", "from decimal_benchmark import run")
print "FLOAT", a.timeit(1)
a = Timer("run('123.345', Decimal)", "from decimal_benchmark import run; from decimal import Decimal")
print "DECIMAL", a.timeit(1)
Outputs:
输出:
FLOAT 0.040635041427
DECIMAL 3.39666790146
采纳答案by Greg Hewgill
回答by Andrew G
You can try cdecimal:
您可以尝试cdecimal:
from cdecimal import Decimal
As of Python 3.3, the cdecimal implementation is now the built-in implementation of the decimal
standard library module, so you don't need to install anything. Just use decimal
.
从 Python 3.3 开始,cdecimal 实现现在是decimal
标准库模块的内置实现,因此您无需安装任何东西。只需使用decimal
.
For Python 2.7, installing cdecimal
and using it instead of decimal
should provide a speedup similar to what Python 3 gets by default.
对于 Python 2.7,安装cdecimal
和使用它而不是decimal
应该提供类似于 Python 3 默认情况下获得的加速。
回答by gimel
You should compare Decimalto Long Integerperformance, not floating point. Floating point is mostly hardware these days. Decimalis used for decimal precision, while Floating Pointis for wider range. Use the decimalpackage for monetary calculations.
您应该将Decimal与Long Integer性能进行比较,而不是浮点数。如今,浮点主要是硬件。Decimal用于十进制精度,而Floating Point用于更广泛的范围。使用小数包进行货币计算。
To quote the decimalpackage manual:
引用十进制包手册:
Decimal numbers can be represented exactly. In contrast, numbers like 1.1 do not have an exact representation in binary floating point. End users typically would not expect 1.1 to display as 1.1000000000000001 as it does with binary floating point.
The exactness carries over into arithmetic. In decimal floating point, "0.1 + 0.1 + 0.1 - 0.3" is exactly equal to zero. In binary floating point, result is 5.5511151231257827e-017. While near to zero, the differences prevent reliable equality testing and differences can accumulate. For this reason, decimal would be preferred in accounting applications which have strict equality invariants.
十进制数可以精确表示。相比之下,像 1.1 这样的数字在二进制浮点数中没有精确的表示。最终用户通常不会期望 1.1 显示为 1.1000000000000001,因为它与二进制浮点数一样。
精确性延续到算术中。在十进制浮点数中,“0.1 + 0.1 + 0.1 - 0.3”正好等于零。在二进制浮点中,结果是 5.5511151231257827e-017。虽然接近于零,但差异阻止了可靠的平等测试,差异可能会累积。出于这个原因,在具有严格等式不变量的会计应用程序中,十进制将是首选。
回答by Joel Santirso
Use cDecimal.
使用cDecimal。
Adding the following to your benchmark:
将以下内容添加到您的基准测试中:
a = Timer("run('123.345', Decimal)", "import sys; import cdecimal; sys.modules['decimal'] = cdecimal; from decimal_benchmark import run; from decimal import Decimal")
print "CDECIMAL", a.timeit(1)
My results are:
我的结果是:
FLOAT 0.0257983528473
DECIMAL 2.45782495288
CDECIMAL 0.0687125069413
(Python 2.7.6/32, Win7/64, AMD Athlon II 2.1GHz)
(Python 2.7.6/32, Win7/64, AMD Athlon II 2.1GHz)
回答by pranjal
python Decimal is very slow, one can use float or a faster implementation of Decimal cDecimal.
python Decimal 非常慢,可以使用 float 或 Decimal cDecimal 的更快实现。