What is the complexity of operations with Java 7 BigInteger?

What is the complexity of the multiply , divide and pow methods in BigInteger currently? The documentation does not mention the complexity of the calculations (and nowhere else).

+6
java complexity-theory java-7 biginteger
source share
3 answers

If you look at the code for BigInteger (comes with the JDK), it seems to me that multiply(..) has O (n ^ 2) (actually the multiplyToLen(..) method). The code for other methods is a bit more complicated, but you can see yourself.

Note: this is for Java 6. I assume that it will not be different in Java 7.

+3
source share

Measure it. Perform operations with linearly increasing operands and draw time on a chart. Remember to warm up the JVM (several runs) to get reliable test results.

If the operations are linear O (n), quadratic O (n ^ 2), polynomial or exponential should be obvious.

EDIT: Although you can give theoretical estimates of the algorithms, they may not be as useful in practice. First of all, complexity does not give a factor. Some linear or sub-quadratic algorithms are simply not useful, because they consume so much time and resources that they are not suitable for the problem (for example, multiplication of the Coppersmith-Winograd matrix). Then your calculations can have all the kludges that you can only detect by experiment. There are preparatory algorithms that do nothing to solve the problem, but to speed up the real solver (matrix conditioning). There are suboptimal implementations. With a longer length, your speed may decrease sharply (there is no cache, moving memory, etc.). Therefore, for practical purposes, I advise you to experiment.

It is best to double the length of the input each time and compare the time. And yes, you will find out if the algorithm has n ^ 1.5 or n ^ 1.8 difficulties. Just four of the input length, and you only need half the time for 1.5 instead of 2. You will again get almost half the time for 1.8, if you multiply the length 256 times.

+2
source share

There is a new β€œbest” BigInteger class that is not used by the jdk sun for conservatism and the lack of useful regression tests (huge datasets). The guy who made the best algorithms could discuss the old BigInteger in the comments.

Here you go http://futureboy.us/temp/BigInteger.java

+1
source share

All Articles