Java loop Performance and java Loop

Source: Internet
Author: User

Java loop Performance and java Loop
For iterator performs the best iteration loop performance, then foreach, then declare the variable in advance, and finally calculate the set size for each iteration.

Package test; import java. util. arrayList; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. map;/*** Performance Test ** @ author LinSir **/public class PerformanceTest {public static void main (String [] args) {// TODO Auto-generated method stub List <Map <String, Object> list = new ArrayList <Map <String, Object> (); Map <String, object> map = new HashMap <String, Object> (); map. put ("name", "jams"); map. put ("sex", true); map. put ("age", 17); for (int I = 0; I <100000; I ++) {list. add (map);} System. out. println ("first start time:"); long a11 = System. currentTimeMillis (); long a1 = System. nanoTime (); for (int I = 0; I <list. size (); I ++) {list. get (I ). get ("name"); list. get (I ). get ("sex"); list. get (I ). get ("age");/* list. get (I ). put ("a", 999); list. get (I ). remove ("sex"); */} long a2 = System. nanoTime (); long a22 = System. currentTimeMillis (); System. out. println ("first nanosecond difference:" + (a2-a1); System. out. println ("first millisecond difference:" + (a22-a11); System. out. println ("-----------------------------"); System. out. println ("Second Start Time:"); long b11 = System. currentTimeMillis (); long b1 = System. nanoTime (); for (Map <String, Object> map2: list) {map2.get ("name"); map2.get ("sex"); map2.get ("age "); /* map2.put ("a", 999); map2.remove ("sex"); */} long b2 = System. nanoTime (); long b22 = System. currentTimeMillis (); System. out. println ("2nd nanoseconds:" + (b2-b1); System. out. println ("2nd milliseconds:" + (b22-b11); System. out. println ("-----------------------------"); System. out. println ("3rd start time:"); long c11 = System. currentTimeMillis (); long c1 = System. nanoTime (); for (Iterator <Map <String, Object> iterator = list. iterator (); iterator. hasNext ();) {Map <String, Object> map2 = (Map <String, Object>) iterator. next (); map2.get ("name"); map2.get ("sex"); map2.get ("age");/* map2.put ("a", 999 ); map2.remove ("sex"); */} long c2 = System. nanoTime (); long c22 = System. currentTimeMillis (); System. out. println ("3rd nanoseconds:" + (c2-c1); System. out. println ("3rd milliseconds:" + (c22-c11); System. out. println ("-----------------------------"); System. out. println ("4th start time:"); long d11 = System. currentTimeMillis (); long d1 = System. nanoTime (); int listSize = list. size (); for (int I = 0; I <listSize; I ++) {list. get (I ). get ("name"); list. get (I ). get ("sex"); list. get (I ). get ("age");/* list. get (I ). put ("a", 999); list. get (I ). remove ("sex"); */} long d2 = System. nanoTime (); long d22 = System. currentTimeMillis (); System. out. println ("4th nanoseconds:" + (d2-d1); System. out. println ("4th milliseconds:" + (d22-d11); System. out. println ("-----------------------------");}}

// Determine whether the for iterator performance is greater than that of the for Loop Based on the preceding time consumption.

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.