In Java, the use of iterative list data is often used in this paper to compare the efficiency of several commonly used formulations. Although there have been similar articles on the Internet, they do not agree with their conclusions.
Common implementation methods:
1.for Loop:
[Java]View Plaincopyprint?
- for (int i = 0; i < list.size (); i++)
- for (int i = 0, size = list.size (); i < size; i++)
The average person would think that the second way of writing is highly efficient.
2.foreach:
[Java]View Plaincopyprint?
- for (Object obj:list)
This is a concise way of writing, you can only read the list, can not be modified.
3.while:
[Java]View Plaincopyprint?
- int size = List.size ();
- while (size--> 0)
4. Iteration:
[Java]View Plaincopyprint?
- Object iter = List.iterator ();
- while (Iter.hasnext ()) {
- Iter.next ();
- }
Test code:
Test code written for several of the above methods.
[Java]View Plaincopyprint?
- Public static void Main (string[] args) {
- list<integer> list = new arraylist<integer> ();
- int runTime = + ; Number of executions
- For (int i = 0; i < i++) {
- List.add (i);
- }
- int size = List.size ();
- Long currtime = System.currenttimemillis (); //System time before start of analysis
- //Basic for
- For (int j = 0; j < RunTime; J + +) {
- For (int i = 0; i < size; i++) {
- List.get (i);
- }
- }
- Long time1 = System.currenttimemillis ();
- //foreach
- For (int j = 0; j < RunTime; J + +) {
- For (Integer integer:list) {
- }
- }
- Long time2 = System.currenttimemillis ();
- For (int j = 0; j < RunTime; J + +) {
- //while
- int i = 0;
- While (i < size) {
- List.get (i++);
- }
- }
- Long Time3 = System.currenttimemillis ();
- For (int j = 0; j < RunTime; J + +) {//normal for loop
- For (int i = 0; i < list.size (); i++) {
- List.get (i);
- }
- }
- Long time4 = System.currenttimemillis ();
- For (int j = 0; j < RunTime; J + +) {//Iteration
- Iterator<integer> iter = List.iterator ();
- While (Iter.hasnext ()) {
- Iter.next ();
- }
- }
- Long time5 = System.currenttimemillis ();
- long time = time1-currtime;
- System.out.print ("Use for:" + time);
- Time = time2-time1;
- System.out.print ("\tuse foreach:" + time);
- Time = time3-time2;
- System.out.print ("\tuse while:" + time);
- Time = Time4-time3;
- System.out.print ("\tuse for2:" + time);
- Time = Time5-time4;
- System.out.print ("\tuse iterator:" + time);
- System.out.println ();
- }
Output result (JDK1.6):
1.
Use for:8695, use, foreach:17091 use, while:6867 use, for2:7741 use iterator:14144
2.
Use for:8432, use, foreach:18126 use, while:6905 use, for2:7893 use iterator:13976
3.
Use for:8584, use, foreach:17177 use, while:6875 use, for2:7707 use iterator:14345
Conclusion:
1. The efficiency of foreach for lists is the lowest:
Time is more than twice times the normal for loop. The individual understands that its implementation should be similar to iterator.
2. List.size () has very little overhead:
The number of list.size () has little effect on efficiency. Looking at the implementation of ArrayList, the size () method simply returns the length attribute within the object, and there is no other calculation, so there is only the overhead of the function call.
Test of an array: The list of lists in the code is replaced with an array for testing (iterator not applicable), and it is found to have a time-consuming base of 0. Description: 3. The Get () method of the list costs a lot
Should be primarily detected when data legitimacy arises.
Increase the number of executions by 1 million times times, you can see the results are basically equal, and there is no obvious difference. Description
4. Array length also has no overhead
The length of the visible array is not calculated every time it is executed. When you think of Java creating an array, you need to specify the length of the array, which is obviously not discarded when compiling the process.
There is a similar article on the Internet, it actually came up with a foreach implementation of the most efficient conclusion. Look at its test code will find a fatal problem, it actually executes each loop when the System.out.print () method calls the array content output, he did not know that the operation time is very large, so that the results of what is useful.
Comparison of the efficiency of several loops in the iterative list of data in Java