Java basic type running time is better than boxed basic type detailed examples
Package com. Valid tive;
Public class PublishTest {
Public static void main (String [] args ){
// TODO Auto-generated method stub
// When the basic types and basic packing operations are mixed in the program, the basic types of packing are automatically split, resulting in longer running time and more space occupied.
// From the three examples, when sum and I are the same as the basic type of long, the running time is the least.
Long startTime = System. currentTimeMillis (); // obtain the start time, in milliseconds
Long sum = 0l ;//
System. out. println ("Integer. MAX_VALUE =" + Integer. MAX_VALUE );
For (long I = 0l; I <= Integer. MAX_VALUE; I ++ ){
Sum + = I;
}
System. out. println ("sum =" + sum );
Long endTime = System. currentTimeMillis (); // get the end time
System. out. println ("program running time:" + (endTime-startTime) + "ms ");
// First: When sum and I are both basic types of long
// Sum = 2305843008139952128
// Running time: 7250 ms
// Third: When sum is the basic type of Long packing and I is the basic type of long packing
// Sum = 2305843008139952128
// Running time: 24622 ms
// Second: When sum and I are both basic types of Long packing
// Sum = 2305843008139952128
// Running time: 60610 ms
}
}