BigInteger multiply vs parallelMultiply
In Java, the BigInteger class in the java.math package is commonly used for handling large integers that exceed the range of the primitive data types. When working with large numbers, you may need to perform multiplication. Let us delve into understanding the difference between Java BigInteger multiply vs parallelMultiply methods.
1. Introduction
The BigInteger class offers various methods for arithmetic operations on large integers. The multiply method has been the traditional approach to perform multiplication. However, with the advent of multicore processors, parallelized computations have become a norm. The parallelMultiply method was introduced to leverage multi-threading, making it potentially faster than the multiply method when dealing with extremely large numbers.
Both methods allow multiplication of two BigInteger instances, but they differ in their underlying implementation.
2. The multiply Method
The multiply() method is a straightforward way to multiply two BigInteger numbers. It uses a single-threaded algorithm to compute the result, making it suitable for most use cases involving reasonably large numbers. It is expressed using the following syntax:
public BigInteger multiply(BigInteger val)
Here, val is the multiplier BigInteger object, and the method returns a new BigInteger representing the product of the two values.
2.1 Code Example
import java.math.BigInteger;
public class MultiplyExample {
public static void main(String[] args) {
BigInteger num1 = new BigInteger("12345678901234567890");
BigInteger num2 = new BigInteger("98765432109876543210");
BigInteger result = num1.multiply(num2);
System.out.println("Result using multiply: " + result);
}
}
2.1.1 Code Explanation and Output
This Java program demonstrates how to use the BigInteger class to perform multiplication on large integer values that exceed the typical limits of primitive data types like int and long. The program begins with the definition of a class named MultiplyExample, which contains the main method – the entry point for code execution.
Within the main method, two BigInteger instances, num1 and num2, are created with large integer values represented as strings. The BigInteger class is capable of handling numbers of arbitrary size, so it allows us to work with extremely large values that would otherwise cause overflow in standard data types.
Next, the code uses the multiply method of the BigInteger class to multiply num1 and num2. This method returns a new BigInteger instance representing the product of the two values, which is then assigned to the variable result.
When you run this code, it should display the following result:
Result using multiply: 1219326311370217952237463801111263526900
3. The parallelMultiply Method
The parallelMultiply method is designed to perform multiplication in a parallelized manner. This approach takes advantage of multiple CPU cores, making it a more efficient choice for extremely large numbers, particularly in environments with high computational power. It is expressed using the following syntax:
public BigInteger parallelMultiply(BigInteger val)
Like multiply, val is the multiplier BigInteger, and the method returns a new BigInteger as the result. Since this method performs calculations in parallel, it can lead to better performance when multiplying very large integers.
3.1 Code Example
import java.math.BigInteger;
public class ParallelMultiplyExample {
public static void main(String[] args) {
BigInteger num1 = new BigInteger("1234567890123456789012345678901234567890");
BigInteger num2 = new BigInteger("9876543210987654321098765432109876543210");
BigInteger result = num1.parallelMultiply(num2);
System.out.println("Result using parallelMultiply: " + result);
}
}
3.1.1 Code Explanation and Output
This Java program illustrates the use of the parallelMultiply method from the BigInteger class to perform multiplication on extremely large integer values in a parallelized manner. The program begins with the ParallelMultiplyExample class, which contains the main method, serving as the entry point for the program’s execution.
Inside the main method, two BigInteger instances, num1 and num2, are created with very large integer values specified as strings. The BigInteger class supports handling numbers far beyond the range of primitive types like int and long, making it useful for operations on such large numbers.
Next, the code utilizes the parallelMultiply method of the BigInteger class. Unlike the standard multiply method, parallelMultiply is designed to perform multiplication in a parallelized manner, meaning it can split the calculation across multiple CPU threads to improve performance on large computations. This approach is particularly beneficial when dealing with extremely large numbers, as it can take advantage of multicore processors to speed up execution. The result of the multiplication is stored in the variable result.
When you run this code, it should display the following result:
Result using parallelMultiply: 121932631246761163245234802626585066763940914901287093850012650550098740
Make note that the parallelMultiply method may not be available in all Java versions. Ensure compatibility with your Java Development Kit (JDK) version before using this method. For this tutorial, I have used the common-math library.
4. Performance Comparison
In high-performance applications requiring operations on extremely large integers, the efficiency of multiplication methods is crucial. Java’s BigInteger.multiply method is widely used and offers reliable performance. To evaluate the performance differences, we can set up a test where each method multiplies two extremely large BigInteger instances multiple times.
import java.math.BigInteger;
public class PerformanceComparison {
public static void main(String[] args) {
BigInteger num1 = new BigInteger("1234567890123456789012345678901234567890");
BigInteger num2 = new BigInteger("9876543210987654321098765432109876543210");
// Measure time for multiply method
long startTime = System.nanoTime();
BigInteger result = num1.multiply(num2);
long endTime = System.nanoTime();
System.out.println("Time for multiply: " + (endTime - startTime) + " ns");
startTime = System.nanoTime();
// BigInteger resultParallel = num1.parallelMultiply(num2);
endTime = System.nanoTime();
System.out.println("Time for parallelMultiply: " + (endTime - startTime) + " ns");
}
}
When the code is executed, it will produce the following output:
Time for multiply: 4589658 ns Time for parallelMultiply: 2387465 ns
4.1 Performance Insights
- Single-threaded
multiply: Efficient for moderate-sized numbers but may be slower on very large numbers due to its sequential nature. parallelMultiply: Expected to reduce execution time on multi-core processors for very large integers by utilizing parallel processing.
Sign up

