Performance comparison of three kinds of traversal mechanism JAVA

I know at this stage, there are three JAVA traversing mechanism

  • for loop
  • forEach loop
  • Iterator cycle

Performance comparison of three kinds of traversal mechanism JAVA

 

JAVA thousands and thousands of data structure, but most of them are based on a data structure of the package, dependent on the comparison HashMap Node array, the bottom is the LinkedList list, the ArrayList array repackaged ...... digress

In conclusion, JAVA underlying data structure, I think there are two

  • Array
  • List

If this is coupled with Hash (Hash operation is not consistent with the arrays and linked lists), that is, three kinds of

Because usually develop data structure after most of them preferred packaging, so I would use the following

  • ArrayList (array after packaging)
  • LinkedList (a linked list after packaging)
  • HashSet (Hash the packed array type)

Differences between these three data structure traversal mechanisms at different times of the time

Some people may HashMap Why not compare it to me, because JAVA design is to achieve a Map, and then realize Set. If you have read the source code will find: Set realize each subclass, Map has a corresponding property after serialization achieved, because search time complexity Hash is O (1), After obtaining the key lookup value of time is roughly the same, so I do not compare HashMap.

Digression

I was reading "crazy JAVA" read: JAVA array of designer interior entry in the Map null value is set so as to realize the Set. Because I is subject to official documentation and source code, specifically I do not know right or wrong, but because the key Hash different from each other, Set elements are also different from each other, so I think this view is correct.

To be fair test, I will take the following limited

  • The size of each data structure are set three of the order
  • 10
  • 100
  • 1000
  • Random number generating elements
  • Traversing operation are output values ​​of the current element

Note: The time overhead influenced by the local environment, the measured values ​​may be changes, but the proportion is generally correct

Compare the ArrayList

  • Code

public class TextArray {

private static Random random;

private static List<Integer> list1;

private static List<Integer> list2;

private static List<Integer> list3;

public static void execute(){

random=new Random();

initArray ();

testForWith10Object ();

testForEachWith10Object ();

testIteratorWith10Object ();

testForWith100Object ();

testForEachWith100Object ();

testIteratorWith100Object ();

testForWith1000Object ();

testForEachWith1000Object ();

testIteratorWith1000Object ();

}

private static void testForWith10Object(){

printFor(list1);

}

private static void testForWith100Object(){

printFor (list2);

}

private static void testForWith1000Object(){

printFor (list3);

}

private static void testForEachWith10Object(){

printForeach(list1);

}

private static void testForEachWith100Object(){

printForeach(list2);

}

private static void testForEachWith1000Object(){

printForeach(list3);

}

private static void testIteratorWith10Object() {

printIterator(list1);

}

private static void testIteratorWith100Object() {

printIterator(list2);

}

private static void testIteratorWith1000Object() {

printIterator(list3);

}

private static void printFor(List<Integer> list){

System.out.println();

System.out.print("data:");

long start=System.currentTimeMillis();

for(int i=0,length=list.size();i<length;i++){

System.out.print(list.get(i)+" ");

}

System.out.println();

long end=System.currentTimeMillis();

System.out.println("for for "+list.size()+":"+(end-start)+"ms");

}

private static void printForeach(List<Integer> list){

System.out.println();

System.out.print("data:");

long start=System.currentTimeMillis();

for(int temp:list){

System.out.print(temp+" ");

}

System.out.println();

long end=System.currentTimeMillis();

System.out.println("foreach for "+list.size()+":"+(end-start)+"ms");

}

private static void printIterator(List<Integer> list){

System.out.println();

System.out.print("data:");

Iterator<Integer> it=list.iterator();

long start=System.currentTimeMillis();

while(it.hasNext()){

System.out.print(it.next()+" ");

}

System.out.println();

long end=System.currentTimeMillis();

System.out.println("iterator for "+list.size()+":"+(end-start)+"ms");

}

private static void initArray(){

list1=new ArrayList<>();

list2=new ArrayList<>();

list3=new ArrayList<>();

for(int i=0;i<10;i++){

list1.add(random.nextInt());

}

for(int i=0;i<100;i++){

list2.add(random.nextInt());

}

for(int i=0;i<1000;i++){

list3.add(random.nextInt());

}

}

}

  • Output (output to ignore elements)
for for 10:1ms
foreach for 10:0ms
iterator for 10:2ms
for for 100:5ms
foreach for 100:4ms
iterator for 100:12ms
for for 1000:33ms
foreach for 1000:7ms
iterator for 1000:16ms
复制代码
  • 10 100 1000 for 1ms 5ms forEach 0ms 4ms Iterator 2ms 12ms
  • in conclusion
  • for the performance of the most unstable, foreach followed, Iterator best
  • Recommendations
  1. In case the amount of data is not clear (probably 1w, 10w, or other), it is recommended to use Iterator to traverse
  2. In the small amount of data to clear and the order when using foreach priority
  3. Need to use the index, using incremental variable cost ratio for the smaller

Compare the LinkedList

  • Code

public class TextLinkedList {

private static Random random;

private static List<Integer> list1;

private static List<Integer> list2;

private static List<Integer> list3;

public static void execute(){

random=new Random();

initList();

testForWith10Object ();

testForEachWith10Object ();

testIteratorWith10Object ();

testForWith100Object ();

testForEachWith100Object ();

testIteratorWith100Object ();

testForWith1000Object ();

testForEachWith1000Object ();

testIteratorWith1000Object ();

}

private static void testForWith10Object() {

printFor(list1);

}

private static void testForEachWith10Object() {

printForeach(list1);

}

private static void testIteratorWith10Object() {

printIterator(list1);

}

private static void testForWith100Object() {

printFor (list2);

}

private static void testForEachWith100Object() {

printForeach(list2);

}

private static void testIteratorWith100Object() {

printIterator(list2);

}

private static void testForWith1000Object() {

printFor (list3);

}

private static void testForEachWith1000Object() {

printForeach(list3);

}

private static void testIteratorWith1000Object() {

printIterator(list3);

}

private static void printFor(List<Integer> list){

System.out.println();

System.out.print("data:");

long start=System.currentTimeMillis();

for(int i=0,size=list.size();i<size;i++){

System.out.print(list.get(i));

}

System.out.println();

long end=System.currentTimeMillis();

System.out.println("for for "+list.size()+":"+(end-start)+"ms");

}

private static void printForeach(List<Integer> list){

System.out.println();

System.out.print("data:");

long start=System.currentTimeMillis();

for(int temp:list){

System.out.print(temp+" ");

}

System.out.println();

long end=System.currentTimeMillis();

System.out.println("foreach for "+list.size()+":"+(end-start)+"ms");

}

private static void printIterator(List<Integer> list){

System.out.println();

System.out.print("data:");

Iterator<Integer> it=list.iterator();

long start=System.currentTimeMillis();

while(it.hasNext()){

System.out.print(it.next()+" ");

}

System.out.println();

long end=System.currentTimeMillis();

System.out.println("iterator for "+list.size()+":"+(end-start)+"ms");

}

private static void initList() {

list1=new LinkedList<>();

list2=new LinkedList<>();

list3=new LinkedList<>();

for(int i=0;i<10;i++){

list1.add(random.nextInt());

}

for(int i=0;i<100;i++){

list2.add(random.nextInt());

}

for(int i=0;i<1000;i++){

list3.add(random.nextInt());

}

}

}

  • Output (output to ignore elements)
for for 10:0ms
foreach for 10:1ms
iterator for 10:0ms
for for 100:1ms
foreach for 100:0ms
iterator for 100:3ms
for for 1000:23ms
foreach for 1000:25ms
iterator for 1000:4ms
  • 10 100 1000 for 0ms 1ms forEach 1ms 0ms Iterator 0ms 3ms
  • in conclusion
  • foreach of the most unstable performance, for second place, Iterator best
  • Recommendations
  1. Try to use Iterator to traverse
  2. Need to use the index, using incremental variable cost ratio for the smaller

Compare the HashSet

Note: Due to Hash traversal algorithm inconsistent with other types, so cancel the comparison of the for loop

  • Code

public class TextHash {

private static Random random;

private static Set<Integer> set1;

private static Set<Integer> set2;

private static Set<Integer> set3;

public static void execute(){

random=new Random();

initHash ();

testIteratorWith10Object ();

testForEachWith10Object ();

testIteratorWith100Object ();

testForEachWith100Object ();

testIteratorWith1000Object ();

testForEachWith1000Object ();

}

private static void testIteratorWith10Object() {

printIterator(set1);

}

private static void testForEachWith10Object() {

printForeach(set1);

}

private static void testIteratorWith100Object() {

printIterator(set2);

}

private static void testForEachWith100Object() {

printForeach(set2);

}

private static void testIteratorWith1000Object() {

printIterator(set3);

}

private static void testForEachWith1000Object() {

printForeach(set3);

}

private static void initHash() {

set1=new HashSet<>();

set2=new HashSet<>();

set3=new HashSet<>();

for(int i=0;i<10;i++){

set1.add(random.nextInt());

}

for(int i=0;i<100;i++){

set2.add(random.nextInt());

}

for(int i=0;i<1000;i++){

set3.add(random.nextInt());

}

}

private static void printIterator(Set<Integer> data){

System.out.println();

System.out.print("data:");

long start=System.currentTimeMillis();

Iterator<Integer> it=data.iterator();

while (it.hasNext()){

System.out.print(it.next()+" ");

}

System.out.println();

long end=System.currentTimeMillis();

System.out.println("iterator for "+data.size()+":"+(end-start)+"ms");

}

private static void printForeach(Set<Integer> data){

System.out.println();

System.out.print("data:");

long start=System.currentTimeMillis();

for(int temp:data){

System.out.print(temp+" ");

}

System.out.println();

long end=System.currentTimeMillis();

System.out.println("foreach for "+data.size()+":"+(end-start)+"ms");

}

}

  • Output (output to ignore elements)
iterator for 10:0ms
foreach for 10:0ms
iterator for 100:6ms
foreach for 100:0ms
iterator for 1000:30ms
foreach for 1000:9ms
  • 10 100 1000 foreach 0ms 0ms Iterator 0ms 6ms
  • in conclusion
  • foreach performance ahead of Iterator
  • Recommendations
  • After the election foreach, and good performance, it is also easy to write.

to sum up

  1. for cycle characteristics in comparison in three generally down to leeward, and larger increment in the cost. Even later when I need to use the index would rather use the increment will not be used for the variables.
  2. Iterator performance in an array and a linked list of performance are the best, JAVA designers should be optimized before. In the case where the response time sensitive (e.g., web response), priority.
  3. foreach performance belongs between the two, under the wording simple and not time-sensitive situation I will try to use.

That is all I compare it to a common data structure traversal mechanism, though it is very preliminary, but from which I have learned a lot, I hope you also gain something.

Guess you like

Origin blog.csdn.net/mifffy_java/article/details/91489707