To Java programmers: Do not use a common for loop To traverse the javaslist and javalinkedlist
Normal for Loop traversal of ArrayList and sorted list
For most Java programmers, the List most commonly used is the ArrayList. For ArrayList traversal, it is generally written as follows:
public static void main(String[] args){ List<Integer> arrayList = new ArrayList<Integer>(); for (int i = 0; i < 100; i++) arrayList.add(i); for (int i = 0; i < 100; i++) System.out.println(arrayList.get(i));}
If you want to use the consumer list in the future, some friends may traverse the consumer list in the same way:
public static void main(String[] args){ List<Integer> linkedList = new LinkedList<Integer>(); for (int i = 0; i < 100; i++) linkedList.add(i); for (int i = 0; i < 100; i++) System.out.println(linkedList.get(i));}
Remember:This is a very bad practice.. This is actually not a Java issue, but a Data Structure issue. I believe that the same is true for languages that are changed from Java to other ones.
The following describes how to test the normal for Cycle Efficiency of ArrayList and explain list and analyze the causes.
Comparison of the traversal speed of ArrayList and sorted list using a common for Loop
First, the test code is provided:
Public class ListIteratorTest {private final static int LIST_SIZE = 1000; public static void main (String [] args) {List <Integer> arrayList = new ArrayList <Integer> (); list <Integer> struct List = new struct List <Integer> (); for (int I = 0; I <LIST_SIZE; I ++) {arrayList. add (I); Lists List. add (I);} long startTime = System. currentTimeMillis (); for (int I = 0; I <arrayList. size (); I ++) arrayList. get (I); System. out. println ("ArrayList traversal speed:" + (System. currentTimeMillis ()-startTime) + "ms"); startTime = System. currentTimeMillis (); for (int I = 0; I <memory list. size (); I ++) into list. get (I); System. out. println ("Upload list traversal speed:" + (System. currentTimeMillis ()-startTime) + "ms ");}}
The LIST_SIZE is constantly increased. I use a table to show the running result:
| |
1000 |
5000 |
10000 |
50000 |
100000 |
| ArrayList |
0 ms |
1 ms |
2 ms |
3 ms |
3 ms |
| Shortlist |
3 ms |
16 ms |
88 ms |
2446 ms |
18848 ms |
From the running results, we can see that increasing the List capacity by multiples makes the ArrayList traversal more stable, and the traversal of the sorted List is almost explosive growth. It is no longer necessary to test it.
The following explains the cause of this phenomenon.
Why does ArrayList use normal for loop traversal?
Let's take a look at the source code of the get method of ArrayList:
public E get(int index) {RangeCheck(index);return (E) elementData[index];}
We can see that the get method of ArrayList only obtains a position element from the array. We have concluded that,The time complexity of the get method of ArrayList is O (1), O (1) means that the time complexity is a constant and has nothing to do with the size of the array. Given the position of the array, the data can be directly located.
Actually, those who are familiar with C, C ++, or have a good understanding of the pointer must understand why. I will explain why it is faster to use get for arrays.
At the bottom layer of the computer, data has addresses, just as people have addresses. Suppose I wrote the following code:
int[3] ints = {1, 3, 5};
In Java, an int type data is 4 bytes. What the computer does in this case is, in the memory space, find a continuous memory space that is sufficient to store 3 4 bytes, that is, 12 bytes of the array, and return the first address of the memory space. For example, if the first address of the memory space is 0x00, then 1 is placed in 0x00 ~ In 0x03, 3 is placed in 0x04 ~ In 0x07, 5 is placed in 0x08 ~ 0x0 B.
At this time, it is very simple. When we take ints [1], the computer will calculate that the data of ints [1] is stored in the memory starting with 0x04 and occupying 4 bytes. Therefore, the computer starts from 0x04 ~ 0x07 read data from the address space.
The whole process has nothing to do with the size of the array. What the computer does isCalculate the starting address --> fetch data from the addressSo we can see that it is fast and stable to use a common for loop to traverse the ArrayList.
Causes of slow traversal by using a common for Loop
Let's take a look at what the get method of the listing list has done:
public E get(int index) { return entry(index).element;}
1 private Entry<E> entry(int index) { 2 if (index < 0 || index >= size) 3 throw new IndexOutOfBoundsException("Index: "+index+ 4 ", Size: "+size); 5 Entry<E> e = header; 6 if (index < (size >> 1)) { 7 for (int i = 0; i <= index; i++) 8 e = e.next; 9 } else {10 for (int i = size; i > index; i--)11 e = e.previous;12 }13 return e;14 }
Because the sorted list is a two-way linked list, the first 6th rows mean to calculate the first half or the second half of I, and then the first half of the second, ignore this and analyze why using a common for loop to traverse the sorted list is so slow.
The reason is 7th ~ 8th rows, 10th ~ The two for statements in the first row are used as an example:
1. get (0), get the address of Node0 with 0 digits directly, and get the data in Node0.
2. get (1), get the address of Node0 with 0 digits directly, find the address of Node1 with the next one digit from Node0 with 0 digits, find Node1, and get the data in node1.
3. get (2), get the address of Node0 with 0 digits directly, find the address of Node1 with the next one digit from Node0 with 0 digits, and find Node1, find the address of Node2 in the next two places from Node1, and find Node2 to get the data in node2.
And so on.
That is to say,The previous list will repeat the previous data when getting data from any location.. If I have 10 data records, I will query 1 + 2 + 3 + 4 + 5 + 5 + 4 + 3 + 2 + 1 = 30 data records, compared to the ArrayList, you only need to query the data 10 times. As the capacity of the consumer list increases, the gap increases. In fact, the number of data queries using the sorted list should be clearly understood, that is, the factorial with half the capacity, that is(0.5N )!, N is the capacity of the sort list.
In other words, (0.5N )! The time complexity is N !, The time complexity rules are as follows:
O (1) <O (log2N) <O (n) <O (N * log2N) <O (N2) <O (N3) <2N <3N <N!
The first four are better, the middle two are average, and the last three are very bad. That is, N! It is the worst time complexity. As long as N is relatively large, the program will basically not run.
Postscript
Based on the above analysis, Java programmers should never use a common for loop to traverse the listing list. You can use the iterator or foreach loop (the principle of foreach loop is that the iterator) to traverse the sorted list. This method is to directly find data by address, which will greatly improve the efficiency of traversing the sorted list.