Performance issues for the for each statement in Java7

Source: Internet
Author: User


About performance issues with the for each statement in Java7

Today I did a full-array algorithm on leetcode , and I had the right results in my eclipse, but when I submitted it to Leetcode, I reported the result of "time Limit exceeded" .

The permutation problem on Leetcode

I began to wonder, in the Leetcode search for a lot of similar problems encountered, but did not find the same as the problems they encountered. Many of them say that at the end of their own code to check out the error. So I went to examine my code problem, and later found that my loop print list of the code to get rid of the re-submitted on the success, then I feel very strange, but there is not sure what is the reason, it is temporarily noted.

See a lot of people at night said Evernote is very useful, originally I have given up Evernote, instead of to know the notes ; but curiosity drove me to try Evernote again, so I went to my impression notes, Suddenly saw a previous collection of a blog, talking about the Java list three kinds of traversal methods performance comparison , I recall today encountered a similar problem. So went to see, and then tested himself, found that alas, really, as bloggers say, Java7 language support of the new grammar, the code is the most concise, but the performance is the worst, and then think about the problems encountered during the day, it is clear.

Here's the code to test the list traversal performance:

Package Org.phn;import Java.util.arraylist;import Java.util.iterator;import Java.util.list;public class testlistprinttime {public static void main (string[] args) {list<string> List = new ARRAYLIST&LT;STRING&G        t; ();        Long T1, T2;        for (int j = 0; J < 10000000; J + +) {List.add ("aaaaaa" + j);        } System.out.println ("List First Visit Method:");        T1 = System.currenttimemillis ();        for (String tmp:list) {//SYSTEM.OUT.PRINTLN (TMP);        } t2 = System.currenttimemillis ();        System.out.println ("Run Time:" + (T2-T1) + "(ms)");        System.out.println ("List Second Visit Method:");        T1 = System.currenttimemillis ();            for (int i = 0; i < list.size (); i++) {list.get (i);        System.out.println (List.get (i));        } t2 = System.currenttimemillis ();        System.out.println ("Run Time:" + (T2-T1) + "(ms)"); System.out.println ("List Third Visit Method:");        Iterator<string> iter = List.iterator ();        T1 = System.currenttimemillis ();            while (Iter.hasnext ()) {iter.next ();        System.out.println (Iter.next ());        } t2 = System.currenttimemillis ();        System.out.println ("Run Time:" + (T2-T1) + "(ms)");    System.out.println ("finished!!!!!!!!"); }}

Results:

It can be seen that the for each statement in Java7 is very convenient and clear, but the performance is several times worse, you use carefully oh. Leave a heart, perhaps some of the problems encountered later is that it caused the OH.

Performance issues for the for each statement in Java7

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.