Java Double vs double:class type vs primitive type
I'm curious about the performance difference between Java classes and primitive types So I created a small benchmark and found that the class type was three to seven times slower than the original type (local OSX is 3x and ideone is 7x)
This is a test:
class Main { public static void main(String args[]) { long bigDTime,littleDTime; { long start = System.nanoTime(); Double d = 0.0; for (Double i = 0.0; i < 1432143.341; i += 0.1) { d += i; } long end = System.nanoTime(); bigDTime = end - start; System.out.println(bigDTime); } { long start = System.nanoTime(); double d = 0.0; for (double i = 0.0; i < 1432143.341; i += 0.1) { d += i; } long end = System.nanoTime(); littleDTime = end - start; System.out.println(littleDTime); } System.out.println("D/d = " + (bigDTime / littleDTime)); } }
http://ideone.com/fDizDu
So why is the double type so slow? Why even implementations allow mathematical operators?
Solution
Because the value is wrapped in objects that need allocation, release, memory management and getters and setters
because auto@R_390_2419 @Designed to allow you to use such wrappers without worrying that they are not pure values Don't you want to have an ArrayList < < double >? Performance is not always necessary, and performance degradation of 3x-7x is acceptable according to the situation Optimization is not always a requirement
In each case, using LinkedList for random access to elements may be excessive, but this does not mean that LinkedList should not be implemented This does not mean that using linked lists for a small amount of random access will not affect performance
One last note: you should let the VM warm up before benchmarking these things