因此,我正在尝试改进.net 4的BigInteger类提供的一些操作,因为这些操作似乎是二次的。我已经做了一个粗略的Karatsuba实现,但它仍然比我预期的要慢。
主要的问题似乎是BigInteger没有提供简单的方法来计算位数,所以我必须使用BigInteger.Log(...,2)。根据Visual Studio的说法,大约80-90%的时间花在计算对数上。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
namespace Test
{
class Program
{
static BigInteger Karatsuba(BigInteger x, BigInteger y)
{
int n = (int)Math.Max(BigInteger.Log(x, 2), BigInteger.Log(y, 2));
if (n <= 10000) return x * y;
n = ((n+1) / 2);
BigInteger b = x >> n;
BigInteger a = x - (b << n);
BigInteger d = y >> n;
BigInteger c = y - (d << n);
BigInteger ac = Karatsuba(a, c);
BigInteger bd = Karatsuba(b, d);
BigInteger abcd = Karatsuba(a+b, c+d);
return ac + ((abcd - ac - bd) << n) + (bd << (2 * n));
}
static void Main(string[] args)
{
BigInteger x = BigInteger.One << 500000 - 1;
BigInteger y = BigInteger.One << 600000 + 1;
BigInteger z = 0, q;
Console.WriteLine("Working...");
DateTime t;
// Test standard multiplication
t = DateTime.Now;
z = x * y;
Console.WriteLine(DateTime.Now - t);
// Test Karatsuba multiplication
t = DateTime.Now;
q = Karatsuba(x, y);
Console.WriteLine(DateTime.Now - t);
// Check they're equal
Console.WriteLine(z == q);
Console.Read();
}
}
}那么,我能做些什么来加快速度呢?
发布于 2011-02-20 12:50:14
为什么要计算所有的比特?
在vb中,我这样做:
<Runtime.CompilerServices.Extension()> _
Function BitLength(ByVal n As BigInteger) As Integer
Dim Data() As Byte = n.ToByteArray
Dim result As Integer = (Data.Length - 1) * 8
Dim Msb As Byte = Data(Data.Length - 1)
While Msb
result += 1
Msb >>= 1
End While
Return result
End Function在C#中,它将是:
public static int BitLength(this BigInteger n)
{
byte[] Data = n.ToByteArray();
int result = (Data.Length - 1) * 8;
byte Msb = Data[Data.Length - 1];
while (Msb != 0) {
result += 1;
Msb >>= 1;
}
return result;
}终于..。
static BigInteger Karatsuba(BigInteger x, BigInteger y)
{
int n = (int)Math.Max(x.BitLength(), y.BitLength());
if (n <= 10000) return x * y;
n = ((n+1) / 2);
BigInteger b = x >> n;
BigInteger a = x - (b << n);
BigInteger d = y >> n;
BigInteger c = y - (d << n);
BigInteger ac = Karatsuba(a, c);
BigInteger bd = Karatsuba(b, d);
BigInteger abcd = Karatsuba(a+b, c+d);
return ac + ((abcd - ac - bd) << n) + (bd << (2 * n));
}调用扩展方法可能会减慢速度,因此这样做可能会更快:
int n = (int)Math.Max(BitLength(x), BitLength(y));仅供参考:使用位长度方法,您还可以计算出比BigInteger方法快得多的对数近似值。
bits = BitLength(a) - 1;
log_a = (double)i * log(2.0);至于访问BigInteger结构的内部UInt32数组,这里有一个技巧。
导入反射命名空间
Private Shared ArrM As MethodInfo
Private Shard Bits As FieldInfo
Shared Sub New()
ArrM = GetType(System.Numerics.BigInteger).GetMethod("ToUInt32Array", BindingFlags.NonPublic Or BindingFlags.Instance)
Bits = GetType(System.Numerics.BigInteger).GetMember("_bits", BindingFlags.NonPublic Or BindingFlags.Instance)(0)
End Sub
<Extension()> _
Public Function ToUInt32Array(ByVal Value As System.Numerics.BigInteger) As UInteger()
Dim Result() As UInteger = ArrM.Invoke(Value, Nothing)
If Result(Result.Length - 1) = 0 Then
ReDim Preserve Result(Result.Length - 2)
End If
Return Result
End Function然后,您可以获得大整数的底层UInteger(),如下所示
Dim Data() As UInteger = ToUInt32Array(Value)
Length = Data.Length 或者另一种方式
Dim Data() As UInteger = Value.ToUInt32Array()请注意,_bits字段信息可用于直接访问BigInteger结构的底层UInteger() _bits字段。这比调用ToUInt32Array()方法更快。但是,当BigInteger B <= UInteger.MaxValue _bits为nothing时。我怀疑,作为优化,当BigInteger适合32位(机器大小)字的大小时,MS return使用本机数据类型执行正常的机器字运算。
我也不能像通常使用反射那样使用_bits.SetValue(B,Data())。为了解决这个问题,我使用了BigInteger(bytes() b)构造函数,它有开销。在字节中,您可以使用不安全的指针操作将UInteger()转换为Byte()。因为VB中没有指针操作,所以我使用Buffer.BlockCopy。以这种方式访问数据时,重要的是要注意,如果设置了bytes()数组的MSB,MS会将其解释为负数。我更希望他们用一个单独的sign字段做一个构造器。字数组将增加一个0字节,以使MSB不被选中
此外,当平方时,你可以进一步提高。
Function KaratsubaSquare(ByVal x As BigInteger)
Dim n As Integer = BitLength(x) 'Math.Max(BitLength(x), BitLength(y))
If (n <= KaraCutoff) Then Return x * x
n = ((n + 1) >> 1)
Dim b As BigInteger = x >> n
Dim a As BigInteger = x - (b << n)
Dim ac As BigInteger = KaratsubaSquare(a)
Dim bd As BigInteger = KaratsubaSquare(b)
Dim c As BigInteger = Karatsuba(a, b)
Return ac + (c << (n + 1)) + (bd << (2 * n))
End Function这消除了乘法算法的每次递归中的2次移位、2次加法和3次减法。
https://stackoverflow.com/questions/2187123
复制相似问题