提高Java的BigInteger性能

Kok*_*zzu 11 java biginteger

如何提高Java的Big Integer的性能?

例如,这个阶乘程序:

import java.math.*;
class Fac {
  public static void main(String[] args) {
    BigInteger i = BigInteger.ONE;
    for(BigInteger z=BigInteger.valueOf(2);z.compareTo(BigInteger.valueOf(99999)) != 0;) {
      i = i.multiply(z);
      z = z.add(BigInteger.ONE);
    }
    System.out.println( i );
  }
}
Run Code Online (Sandbox Code Playgroud)

该计划在31.5s 完成

C++中的位置:

#include <iostream>
#include <gmpxx.h>
using namespace std;
int main() {
  mpz_class r;
  r = 1;
  for(int z=2;z<99999;++z) {
    r *= mpz_class(z);
  }
  cout << r << endl;
}
Run Code Online (Sandbox Code Playgroud)

1.0s 完成

和Ruby(用于比较):

puts (2...99999).inject(:*)
Run Code Online (Sandbox Code Playgroud)

4.4s(Ruby)和32.2JRuby中完成

还有Go(用于比较):

package main
import (
 "fmt"
 "math/big"
)
func main() {
  i := big.NewInt(1);
  one := big.NewInt(1)
  for z := big.NewInt(2); z.Cmp(big.NewInt(99999)) < 0;  {
      i.Mul(i,z);
      z.Add(z,one)
  }
  fmt.Println( i );
}
Run Code Online (Sandbox Code Playgroud)

1.6s和0.7s 完成MulRange

编辑按要求:

import java.math.*;
class F2 {
  public static void main(String[] args) {
    BigInteger i = BigInteger.ONE, r = BigInteger.valueOf(2);
    for(int z=2; z<99999 ; ++z) {
      i = i.multiply(r);
      r = r.add(BigInteger.ONE);
    }
    System.out.println( i );
  }
}
Run Code Online (Sandbox Code Playgroud)

运行时间:31.4s

对那些仍然认为第一个和第二个java代码不公平的人编辑2 ..

import java.math.*;
class F3 {
  public static void main(String[] args) {
    BigInteger i = BigInteger.ONE;
    for(int z=2; z<99999 ; ++z) {
      i = i.multiply(BigInteger.valueOf(z));
    }
    System.out.println( i );
  }
}
Run Code Online (Sandbox Code Playgroud)

31.1s 完成

编辑3 @OldCurmudgeon评论:

import java.math.*;
import java.lang.reflect.*;
class F4 {
  public static void main(String[] args) {
    try {
      Constructor<?> Bignum = Class.forName("java.math.MutableBigInteger").getDeclaredConstructor(int.class);
      Bignum.setAccessible(true);
      Object i = Bignum.newInstance(1);
      Method m = i.getClass().getDeclaredMethod("mul", new Class[] { int.class, i.getClass()});
      m.setAccessible(true);
      for(int z=2; z<99999 ; ++z) {
        m.invoke(i, z, i);
      }
      System.out.println( i );
    } catch(Exception e) { System.err.println(e); } 
  }
}
Run Code Online (Sandbox Code Playgroud)

23.7s 完成

编辑4正如@ Marco13所述,最大的问题是字符串创建不在BigInteger本身上.

  • BigInteger:3.0s
  • MutableBigInteger hack:10.1s
  • 字符串创建:~ 20s

Mar*_*o13 3

计算本身不应该花费这么长时间。但是,字符串创建可能需要一段时间。

该程序(感谢 OldCurmudgeon 和/sf/answers/600823191/)在 Core I7、3GHz、Java 7/21 上启动时大约需要 3.9 秒-Xmx1000m -sever

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class FastBigInteger
{
    public static void main(String[] args)
    {
        try
        {
            Class<?> c = Class.forName("java.math.MutableBigInteger");
            Constructor<?> con = c.getDeclaredConstructor(int.class);
            con.setAccessible(true);
            Object i = con.newInstance(1);
            Method m = c.getDeclaredMethod("mul", new Class[] { int.class, c });
            m.setAccessible(true);
            long before = System.nanoTime();
            for (int z = 2; z < 99999; ++z)
            {
                m.invoke(i, z, i);
            }
            long after = System.nanoTime();
            System.out.println("Duration "+(after-before)/1e9);

            String s = i.toString();
            int n = s.length();
            int lineWidth = 200;
            for (int j=0; j<n; j+=lineWidth)
            {
                int j0 = j;
                int j1 = Math.min(s.length(), j+lineWidth);
                System.out.println(s.substring(j0, j1));
            }
        }
        catch (Exception e)
        {
            System.err.println(e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

打印实际计算的持续时间后,需要相当长的时间才能完成字符串的创建,但这里几乎不应该考虑这一点。

这仍然不是一个明智的基准,但表明计算本身至少没有问题。

但不可否认的是,当仅使用而BigInteger不是这个MutableBigIntegerhack 时,它需要 appx. 15秒,与C++实现相比相当差。