有没有人知道python中更快的十进制实现.如下例所示,标准python decimal比float慢约100倍.
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)
FLOAT 0.040635041427
DECIMAL 3.39666790146
Run Code Online (Sandbox Code Playgroud)
谢谢,马克西姆
And*_*w G 21
你可以试试cdecimal:
from cdecimal import Decimal
Run Code Online (Sandbox Code Playgroud)
从Python 3.3开始,cdecimal实现现在是decimal标准库模块的内置实现,因此您不需要安装任何东西.只是用decimal.
对于Python 2.7,安装cdecimal和使用它decimal应该提供类似于Python 3默认获得的加速.