Java for loop efficiency test

Source: Internet
Author: User

Very strange result!

。。!


Test methods such as the following

public class Main {public static void main (string[] args) {        Long ti = System.currenttimemillis ();        for (int i=integer.min_value;i<integer.max_value;i++) {            if (i==i) continue;        }        System.out.println (System.currenttimemillis ()-ti);                TI = System.currenttimemillis ();        for (int i=integer.min_value;i<integer.max_value;i++) {            continue;        }        System.out.println (System.currenttimemillis ()-ti);}}

Output results
61320


Question 1:

After adding inferences to the For loop, the loop runs at a much worse time.


The above is the problem, look at the following test, you will find more interesting

public class Main {public static void main (string[] args) {                Long ti = System.currenttimemillis ();        Long c = 0;        for (int i=integer.min_value;i<integer.max_value;i++) {            C + +;            if (i==i) continue;        }        System.out.println (System.currenttimemillis ()-ti + "  " +c);                TI = System.currenttimemillis ();        c=0;        for (int i=integer.min_value;i<integer.max_value;i++) {            C + +;            Continue;        }        System.out.println (System.currenttimemillis ()-ti+ "  " +c);}}
Same output

  42949672957847  4294967295 But in the loop did a + + operation, time-consuming incredibly added nearly 10 times times! I have to say, java!. The efficiency of your execution is dire.

。。

。

Java version number: Javac 1.7.0_25



Explanation:

In the for loop to increase the inference and without inference, run time difference why the difference is 200 times times?!

-------------------------------------------------------------------------------------------------

Try again today, the Java version number does not know when it became this: Javac 1.7.0_55 results are no longer the same as the last Test, it seems that the last time a bug was encountered.

。

Don't know Java when you upgrade yourself.


Copyright notice: This article Bo Master original article. Blogs, without consent, may not be reproduced.

Java for loop efficiency test

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.