Increasing Java's BigInteger performance

半腔热情 提交于 2019-12-03 02:41:53
Marco13

The computation itself should not take so long. The string creation may take a while, however.

This program (Kudos to OldCurmudgeon and https://stackoverflow.com/a/8583188/823393 ) takes approximately 3.9 seconds on a Core I7, 3GHz, Java 7/21, when started with -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);
        }
    }
}

After printing the duration for the actual computation, it takes quite a while until it finished creating the string, but this should hardly be taken into account here.

This is still not a sensible benchmark, but shows that there is at least no problem with the computation itself.

But admittedly, when using only BigInteger instead of this MutableBigInteger hack, it takes appx. 15 seconds, which is rather poor compared to the C++ implementation.

Start with:

import java.math.*;
class Fac {
  public static void main(String[] args) {
    BigInteger i = BigInteger.ONE;
    BigInteger maxValue = BigInteger.valueOf(99999);

    for(BigInteger z=BigInteger.valueOf(2); z.compareTo(maxValue) != 0;) {
      i = i.multiply(z);
      z = z.add(BigInteger.ONE);
    }

    System.out.println( i );
  }
}

.valueOf source

1081    public static BigInteger More ...valueOf(long val) {
1082        // If -MAX_CONSTANT < val < MAX_CONSTANT, return stashed constant
1083        if (val == 0)
1084            return ZERO;
1085        if (val > 0 && val <= MAX_CONSTANT)
1086            return posConst[(int) val];
1087        else if (val < 0 && val >= -MAX_CONSTANT)
1088            return negConst[(int) -val];
1089
1090        return new BigInteger(val);
1091    }

It will create a new BigInteger everytime since MAX_CONSTANT is 16.


I think it could go slower because the GC starts to collect some older BigInteger instances but anyway you should always use int and long.. here BigInteger is not really needed.

After your last test i think we can be sure it could be caused by the GC.

I have some clojure code calculating the 100 000th fibonacci number using big integers. Now this thread is not about clojure but as clojure runs on the JVM and I ran benchmarks on some of the existing big integer implementations I felt a comment here might be valuable.

The algorithm when it is using the JVM BigInteger class (denoted by the xN literal syntax in clojure) looks as follows:

(defn fibo [n]
  (loop [i n a 1N b 1N]
    (if (> i 0)
      (recur (dec i) b (+ a b))
      a)))

I have re-implemented this using four big integer implementations and I ran benchmarks using the clojure criterium library which does warm ups and some statistical analysis to try to get to somewhat relevant numbers.

Results on my 2,8 GHz Intel Core i7 macbook:

now I realize this is all anecdotal and that we are only measuring addition here, but I would have to say that the huldra catch phrase "Outperforming BigInteger since 2015" seems pretty accurate in this case.

Any comments with pointers to potential candidates for faster big int addition algorithms are much appreciated.

Pinaki Mukherjee

Other answers are related to tune the performance using code.

If you are using java version less than 1.8.0_151, you can tune big integer performance by using the following command options:

-XX:+UseMontgomerySquareIntrinsic
-XX:+UseMontgomeryMultiplyIntrinsic
-XX:+UseSquareToLenIntrinsic
-XX:+UseMultiplyToLenIntrinsic

After 1.8.0_151, these options are turned on by default.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!