I decided to see the difference in the processing speed of the for and for-each cycles.

long start,end = 0; start = System.currentTimeMillis(); ArrayList<Long> al = new ArrayList<>(); for(long a = 0; a < 10000000; a++) { al.add(a);} System.out.println("ArrayList size = "+ al.size()); for(long b : al){end = System.currentTimeMillis();} System.out.println("Выполнение for-each = " + (end-start)+" mc"); start = System.currentTimeMillis(); for(long a = 0; a < al.size(); a++) { end = System.currentTimeMillis(); } System.out.println("Стандартный цикл = " + (end-start)+" mc"); 

Tolley, I wrote something wrong or the speed is really very different. Got on exit:

 ArrayList size = 10000000 Выполнение for-each = 10616 mc Стандартный цикл = 475 mc 

The numbers are understandably different, but the difference between them is always almost three times.

Therefore, I want to know, it turns out that the classical cycle works three times faster or made a mistake in the program?

Even if you remake it like this:

 start = System.currentTimeMillis(); for(long b : al); end = System.currentTimeMillis(); System.out.println("Выполнение for-each = " + (end-start)+" mc"); start = System.currentTimeMillis(); for(long a = 0; a < al.size(); a++); end = System.currentTimeMillis(); 

The difference still turns out big.

  • one
    Because, in the first case, you iterate over the elements of the array, in the second case, you simply increase the measured a to the size of the array. - Vartlok
  • one
    You do not test the speed of the cycle, and the speed of the cycle + call System.currentTimeMillis (). Since the price ratio of these two calls is not known, your reasoning about the "three times" does not make sense. But in any case - the usual cycle is simple, and foreach may require copying the object. - KoVadim
  • 2
    Among other things, you start captures the filling of an array as for a for-each loop. - Alex Krass
  • Pay attention when assignment of start values ​​occurs - Sergey
  • one
    foreach , by the way, works through an iterator. for(Iterator<Long> i = a1.iterator(); i.hasNext(); ) { ... } - Sergey

1 answer 1

In the first piece of code, after fixing the start time, you create an ArrayList, fill it in a loop, and then loop through for-each at each iteration and define end. And after that you try to compare with the speed of the increment of the variable a from 0 to the size of an ArrayList with again determined end at each iteration. In the second cycle, access to the collection is not performed. That is, you measured the speed of two cycles with access to the collection as well as automatic expansion when adding elements to it and simple perekchelkivanie variable. You obviously tried to do that.

In the second piece of code is closer to the truth, but again not that.

 for(long a = 0; a < al.size(); a++); 

Here you went from 0 to the size of the collection, but did not get access to its elements. Change to the following:

 long b; for(int a = 0; a < al.size(); a++) { b = al.get(a); }; 
  • GreyGoblin, this code does not pass. Not applicable. Access to the ArrayList is only available through for-each .. - Kamenev_D
  • The index must be int simply. Make the second cycle like this: for(int a = 0; a < al.size(); a++) - iksuy
  • @Kamenev_D I'm sorry, I copied your code without changing the type of the variable. Corrected the answer. - GreyGoblin
  • @iksuy thanks. I need to not be lazy to check the answers. You can make a mistake even in simple code. - GreyGoblin
  • one
    I did this: long i; for (long a = 0; a <al.size (); a ++) {i = al.get ((int) a);} As a result, the speed is almost the same. It turns out that the speed of cycles is not much different. - Kamenev_D