我是 DecInt(十进制整数)库的作者,所以我会发表一些评论。
DecInt 库专门设计用于处理需要转换为十进制格式的非常大的整数。转换为十进制格式的问题是大多数任意精度的库都以二进制形式存储值。这对于利用内存来说是最快和最有效的,但从二进制转换为十进制通常很慢。Python 的二进制到十进制转换使用 O(n^2) 算法,并且速度非常慢。
DecInt 使用大的十进制基数(通常为 10^250)并将非常大的数字存储在 250 位的块中。将一个非常大的数字转换为十进制格式现在在 O(n) 中运行。
Naive, or grade school, multiplication has a running time of O(n^2). Python uses Karatsuba multiplication which has running time of O(n^1.585). DecInt uses a combination of Karatsuba, Toom-Cook, and Nussbaumer convolution to get a running time of O(n*ln(n)).
Even though DecInt has much higher overhead, the combination of O(n*ln(n)) multiplication and O(n) conversion will eventually be faster than Python's O(n^1.585) multiplication and O(n^2) conversion.
Since most computations don't require every result to be displayed in decimal format, almost every arbitrary-precision library uses binary since that makes the computations easier. DecInt targets a very small niche. For large enough numbers, DecInt will be faster for multiplication and division than native Python. But if you are after pure performance, a library like GMPY will be the fastest.
我很高兴您发现 DecInt 很有帮助。