Is there a faster alternative to python's Decimal?

前端 未结 5 715
野趣味
野趣味 2021-02-05 09:51

Does anyone know of a faster decimal implementation in python?

As the example below demonstrates, the standard library\'s decimal module is ~100 times slower than

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-05 10:42

    Use 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)

提交回复
热议问题