Skip to content

Instantly share code, notes, and snippets.

@WonderCsabo
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WonderCsabo/61cde4619d935bc5cbd4 to your computer and use it in GitHub Desktop.
Save WonderCsabo/61cde4619d935bc5cbd4 to your computer and use it in GitHub Desktop.
Foreach test
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
for (int i = 0; i < 100000000; ++i) {
list.add(null);
}
long start = System.nanoTime();
for (int i = 0; i < 10; ++i) {
for (Object object : list) {
}
}
System.out.println("foreach: " + (System.nanoTime() - start) / 1000);
start = System.nanoTime();
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 100000000; ++j) {
Object object = list.get(j);
}
}
System.out.println("index: " + (System.nanoTime() - start) / 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment