Slow floating point comparisons
Hi,
I've noticed that there's a strange performance cap when you use floating points numbers close to the maximum of double/float.
Here my testcode for floats:
float min = 1.4e-38f;
// float min = 1.4e-39f; < evil
float max = 5;
float test = 1.499f;
boolean b;
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++){
b = (test >= min && test <= max);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
This will run quite fast. But if you change min to "1.4e-39f" it will about as 20 times slower.
Same with doubles:
double min = 4.9e-308;
// double min = 4.9e-309; < evil
double max = 5;
double test = 1.499;
boolean b;
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++){
b = (test >= min && test <= max);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
With min=4.9e-308 it's fast, with min=4.9e-309 it's about 20 times slower.
If you start the vm with "-server" everything runs as fast as expected. First of all, can anyone tell me why there's this performance gap? And what exactly does "-server" to increase the performance like this?!
Thanks
Stefan

