The extended for statement is faster than the normal for statement because it uses Iterator! Someone told me that I left it as it was without checking it. At this time, I decided to investigate it properly. What if it comes to the story that a normal for statement is better ... I think the era is stream, but since my site is Java 6, I have to teach my juniors how to write in Java 6. ~~ It's not because I can't write a stream. ~~
When I did it at around 100,000, the value was too small to compare, so I looped it 10 million times.
Main.java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10000000; i++) {
list.add(i);
}
//start
long start = System.currentTimeMillis();
//10 million loops
for (int i = 0; i < list.size(); i++) {
int tmp = list.get(i);
}
//End
long end = System.currentTimeMillis();
System.out.println("for statement: " + (end - start) + " ms");
//start
start = System.currentTimeMillis();
//10 million loops
for (Integer i : list) {
int tmp = i;
}
//End
end = System.currentTimeMillis();
System.out.println("Extended for statement: " + (end - start) + " ms");
}
}
Such an idiot ... I tried it several times, but the extended for statement didn't get faster.
for statement: 24 ms
Extended for statement: 29 ms
When I thought that it wouldn't end as it was, there was a site like this.
Reason for using extended for statement-[Seasar] Diary of soichirooooo5
If you look at the link, the answer you wanted to write in this article is written, It's a big deal, so I'll do it till the end. Since the verification is done only with ArrayList, I will also verify with Array and LinkedList.
Main.java
public class Main {
public static void main(String[] args) {
int[] array = new int[10000000];
for (int i = 0; i < 10000000; i++) {
array[i] = i;
}
//start
long start = System.currentTimeMillis();
//10 million loops
for (int i = 0; i < array.length; i++) {
int tmp = array[i];
}
//End
long end = System.currentTimeMillis();
System.out.println("for statement: " + (end - start) + " ms");
//start
start = System.currentTimeMillis();
//10 million loops
for (int i : array) {
int tmp = i;
}
//End
end = System.currentTimeMillis();
System.out.println("Extended for statement: " + (end - start) + " ms");
}
}
~~ for sentence too fast www ~~ Because autoboxing was done in the extended for statement I couldn't measure it accurately. The result remains the same.
for statement: 3 ms
Extended for statement: 3 ms
Main.java
package qiita;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
for (int i = 0; i < 10000000; i++) {
list.add(i);
}
//start
long start = System.currentTimeMillis();
//10 million loops
for (int i = 0; i < list.size(); i++) {
int tmp = list.get(i);
}
//End
long end = System.currentTimeMillis();
System.out.println("for statement: " + (end - start) + " ms");
//start
start = System.currentTimeMillis();
//10 million loops
for (Integer i : list) {
int tmp = i;
}
//End
end = System.currentTimeMillis();
System.out.println("Extended for statement: " + (end - start) + " ms");
}
}
I haven't received a response for about 5 minutes.
Since it does not end, re-verify with 100,000 loops
Main.java
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
for (int i = 0; i < 100000; i++) {
list.add(i);
}
//start
long start = System.currentTimeMillis();
//10 million loops
for (int i = 0; i < list.size(); i++) {
int tmp = list.get(i);
}
//End
long end = System.currentTimeMillis();
System.out.println("for statement: " + (end - start) + " ms");
//start
start = System.currentTimeMillis();
//10 million loops
for (Integer i : list) {
int tmp = i;
}
//End
end = System.currentTimeMillis();
System.out.println("Extended for statement: " + (end - start) + " ms");
}
}
it's amazing! !!
for statement: 5695 ms
Extended for statement: 36 ms
It turned out that the ordinary for statement is faster for things that operate in order from the front, such as ArrayList and arrays. However, the Linked List made an overwhelming difference. It's strange to care about the entity when looping the List interface, so I think I've come to the conclusion that an extended for statement is fine.
The following is a quote from the reference site.
If you use the extended for statement, you don't have to worry about "99% or more performance degradation" even if you don't know whether the object passed to the for statement is an ArrayList or a LinkedList.
Also, the extended for statement converts the implementation type of the List interface into code that uses an iterator, and in the case of an array, it converts it into code that accesses the index.
You can see that the translation of this compiler will generate the appropriate code for the list or array you pass to the for statement. In other words, if you use Extended for, you don't have to think about anything and you don't have to worry about anything. If something goes wrong, it will do something behind the API.
In other words, you can use the extended for statement without thinking about anything. stream Let's study.
Recommended Posts