C#中的大整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/176775/
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
Big integers in C#
提问by Matthew Scharley
Currently I am borrowing java.math.BigInteger
from the J# libraries as described here. Having never used a library for working with large integers before, this seems slow, on the order of 10 times slower, even for ulong
length numbers. Does anyone have any better (preferably free) libraries, or is this level of performance normal?
目前我正在从 J# 库中借用java.math.BigInteger
,如此处所述。以前从未使用过处理大整数的库,这似乎很慢,慢了 10 倍,即使对于ulong
长度数字也是如此。有没有人有更好的(最好是免费的)库,或者这种性能水平正常吗?
采纳答案by Davorin
As of .NET 4.0 you can use the System.Numerics.BigInteger class. See documentation here: http://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx
从 .NET 4.0 开始,您可以使用 System.Numerics.BigInteger 类。请参阅此处的文档:http: //msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx
Another alternative is the IntXclass.
另一种选择是IntX类。
IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - O(N * log N) - multiplication/division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc.
IntX 是一个用纯 C# 2.0 编写的任意精度整数库,具有快速 - O(N * log N) - 乘法/除法算法实现。它提供了对整数的所有基本操作,如加法、乘法、比较、按位移位等。
回答by Daniel Plaisted
I'm not sure about the performance, but IronPython also has a BigInteger class. It is in the Microsoft.Scripting.Math namespace.
我不确定性能,但 IronPython 也有一个 BigInteger 类。它位于 Microsoft.Scripting.Math 命名空间中。
回答by Bill K
Yes, it will be slow, and 10x difference is about what I'd expect. BigInt uses an array to represent an arbitrary length, and all the operations have to be done manually (as opposed to most math which can be done directly with the CPU)
是的,它会很慢,而且 10 倍的差异是我所期望的。BigInt 使用数组来表示任意长度,并且所有操作都必须手动完成(与大多数数学运算可以直接用 CPU 完成相反)
I don't even know if hand-coding it in assembly will give you much of a performance gain over 10x, that's pretty damn close. I'd look for other ways to optimize it--sometimes depending on your math problem there are little tricks you can do to make it quicker.
我什至不知道在汇编中手动编码是否会给你带来超过 10 倍的性能提升,这非常接近。我会寻找其他方法来优化它——有时根据你的数学问题,你可以做一些小技巧来让它更快。
回答by Jason Hymanson
I used Bigintegerat a previous job. I don't know what kind of performance needs you have. I did not use it in a performance-intensive situation, but never had any problems with it.
我在以前的工作中使用过Biginteger。不知道你有什么样的性能需求。我没有在性能密集型的情况下使用它,但从来没有遇到任何问题。
回答by Powerlord
This may sound like a strange suggestion, but have you tested the decimaltype to see how fast it works?
这听起来像是一个奇怪的建议,但是您是否测试过十进制类型以查看它的工作速度?
The decimal range is ±1.0 × 10^?28 to ±7.9 × 10^28, so it may still not be large enough, but it is larger than a ulong.
小数范围是±1.0×10^?28到±7.9×10^28,所以可能还是不够大,但是比一个ulong大。
There was supposed to be a BigInteger class in .NET 3.5, but it got cut.
.NET 3.5 中应该有一个 BigInteger 类,但它被删减了。
回答by Sam Saffron
I reckon you could optimize the implementation if you perform all the operations on BigInts that are going to return results smaller than a native type (Eg. int64) on the native types and only deal with the big array if you are going to overflow.
我认为,如果您在 BigInt 上执行所有操作,这些操作将返回小于本机类型的本机类型(例如 int64)的结果,并且仅在要溢出时才处理大数组,那么您可以优化实现。
editThis implementation on codeproject, seems only 7 times slower ... But with the above optimization you could get it to perform almost identically to native types for small numbers.
编辑codeproject 上的此实现,似乎只慢了 7 倍......但是通过上述优化,您可以获得与小数字的本机类型几乎相同的性能。
回答by technophile
This won't help you, but there was supposed to be a BigInteger class in .Net 3.5; it got cut, but from statements made at PDC, it will be in .Net 4.0. They apparently have spent a lot of time optimizing it, so the performance should be much better than what you're getting now.
这对您没有帮助,但是 .Net 3.5 中应该有一个 BigInteger 类;它被删减了,但从 PDC 的声明来看,它将在 .Net 4.0 中。他们显然花了很多时间优化它,所以性能应该比你现在得到的要好得多。
Further, this question is essentially a duplicate of How can I represent a very large integer in .NET?
此外,这个问题本质上是如何在 .NET 中表示一个非常大的整数?
回答by Scott Dorman
回答by Steve Severance
F#
also ships with one. You can get it at Microsoft.FSharp.Math
.
F#
还附带一个。你可以在Microsoft.FSharp.Math
.
回答by Rasmus Faber
The System.Numerics.BigInteger
class in .NET 4.0 is based on Microsoft.SolverFoundation.Common.BigInteger
from Microsoft Research.
System.Numerics.BigInteger
.NET 4.0 中的类基于Microsoft.SolverFoundation.Common.BigInteger
Microsoft Research。
The Solver Foundation's BigInteger
class looks very performant. I am not sure about which license it is released under, but you can get it here(download and install Solver Foundation and find the Microsoft.Solver.Foundation.dll).
Solver Foundation 的BigInteger
类看起来非常高效。我不确定它是根据哪个许可证发布的,但您可以在此处获取(下载并安装 Solver Foundation 并找到 Microsoft.Solver.Foundation.dll)。