In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what is the difference between the performance of the for loop and the foreach loop in Java. I hope you will get something after reading this article. Let's discuss it together.
Performance comparison between for cycle and foreach cycle
A problem found in the company's codereview process is that some people recycle ordinary for loops and some people use foreach loops. What's the difference between them? When should I use these two loops?
The syntax format of two loops:
The ordinary for loop syntax for (int I = 0; I < integers.length; iTunes +) {System.out.println (intergers [I]);} foreach loop syntax for (Integer in: integers) {System.out.println (in);}
Today we compare the loop performance of two for loops to ArrayList and LinkList collections. First, take a brief look at the difference between ArrayList and LinkList:
ArrayList:ArrayList saves objects in the form of an array, which puts objects in continuous memory blocks, so it is more troublesome to insert and delete, and it is more convenient to query.
LinkList:LinkList is to put objects in a separate space, and each space also stores the index of the next space, that is, the linked list structure in the data structure, which is more convenient to insert and delete, but it is troublesome to find it, so you have to start traversing from the first.
Here is the code I tested:
Public class Main {public static void main (String [] args) {/ / instantiate arrayList List arrayList = new ArrayList (); / / instantiate linkList List linkList = new LinkedList (); / / insert 100000 data for (int I = 0; I < 1000000; iSum +) {arrayList.add (I); linkList.add (I);} int array = 0; / / cycle arrayList long arrayForStartTime = System.currentTimeMillis () with for; for (int I = 0; I < arrayList.size ()) ITunes +) {array = arrayList.get (I);} long arrayForEndTime = System.currentTimeMillis (); System.out.println ("it takes time to cycle arrayList 100000 times with for: + (arrayForEndTime-arrayForStartTime) +" millisecond "); / / cycle arrayList long arrayForeachStartTime = System.currentTimeMillis () with foreach; for (Integer in: arrayList) {array = in;} long arrayForeachEndTime = System.currentTimeMillis () System.out.println ("cycle arrayList 100000 times with foreach: + (arrayForeachEndTime-arrayForeachStartTime) +" millisecond "); / / cycle linkList long linkForStartTime = System.currentTimeMillis (); int link = 0; for (int I = 0; I < linkList.size (); iSEC +) {link = linkList.get (I);} long linkForEndTime = System.currentTimeMillis () System.out.println ("it takes time to cycle linkList 100000 times with for: + (linkForEndTime-linkForStartTime) +" millisecond "); / / cycle linkList long linkForeachStartTime = System.currentTimeMillis () with froeach; for (Integer in: linkList) {link = in;} long linkForeachEndTime = System.currentTimeMillis (); System.out.println (" time to cycle linkList 100000 times with foreach: + (linkForeachEndTime-linkForeachStartTime) + "millisecond");}}
When you loop 100000 times, the console prints the result:
It takes 100000 times to cycle arrayList with for: 5 milliseconds
Cycle arrayList 100000 times with foreach time: 7 milliseconds
It takes 100000 times to cycle linkList with for: 4481 milliseconds
It takes 100000 times to cycle linkList with foreach: 5 milliseconds
As you can see, the normal for loop takes a little less time than the foreach loop when looping ArrayList, and the normal for loop takes much more time than the foreach loop when cycling LinkList.
When I increase the number of loops to a million, the loop ArrayList, the normal for loop is still a little faster than the foreach loop, but when the ordinary for loop is looping LinkList, the program simply freezes.
Conclusion: when the data of circular array structure is needed, it is recommended to use ordinary for loop, because the for loop uses subscript access, for array structure data, it is better to use subscript access.
When you need data with a circular linked list structure, be sure not to use a normal for loop, which is very bad, and may cause the system to crash when the amount of data is large.
Comparison of loop efficiency between for and foreach for arrays
The for loop is slightly slower than foreach
For linked lists
For loops are much slower than foreach. Because every time the for loop acquires the linked list node, it has to start searching from the beginning, while foreach uses an iterative way to find the node, which only needs to be traversed once, saving a lot of time.
After reading this article, I believe you have a certain understanding of "what is the difference between the performance of for loop and foreach loop in Java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.