userimage

Java JRE fatal error: too many multiplications

I was trying to test speed of Math.pow() against multiplication "by hand" and stumbled upon this error:


A fatal error has been detected by the Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000005ac46888, pid=1508, tid=6016

JRE version: Java(TM) SE Runtime Environment (8.0_25-b18) (build 1.8.0_25-b18)
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode windows-amd64 compressed oops)
Problematic frame:
V [jvm.dll+0x496888]

Failed to write core dump. Minidumps are not enabled by default on client versions of Windows


code:

long t = System.nanoTime();

for (int i = 0; i < 10000000; i++) {

    double val = i * i * i * i * i /* 256 times *i */ * i * i * i;

    sum ^= Double.doubleToLongBits(val);

}

System.out.println((System.nanoTime() - t) / 10000000);

userimage

I am assuming that JVM has not sufficient memory to proceed and multiply further because your code running on the fly since for some seconds in memory. Better you can use the below workable code to proceed further WRT tesing of  speed of Math.pow() against multiplication.


package com.test.code;

public class App {
static void mathPowerTest() {
double res = 0;
for (int i = 0; i < 333333333; i++) {
res = Math.pow(i, 30); // pow(i,30)
}
System.out.println("Math.Pow: " + System.nanoTime() + " ms: " + res);
}

static void multiplicationTest() {
double res = 0;
for (int i = 0; i < 333333333; i++) {
res = MyPow(i, 30);
}
System.out.println("MyPow: " + System.nanoTime() + " ms: " + res);
}

static double MyPow(double num, int exp) {
double result = 1.0;
while (exp > 0) {
if (exp % 2 == 1)
result *= num;
exp >>= 1;
num *= num;
}

return result;
}
public static void main(String[] args) {
mathPowerTest();
multiplicationTest();
}

}

Answer is