Java basic type running time is better than boxed basic type detailed examples

Source: Internet
Author: User

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
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.