Home > Back-end >  JDK1.8 foreach loop efficiency than for loop, why did it occur???????
JDK1.8 foreach loop efficiency than for loop, why did it occur???????

Time:09-27

Good god, I tested the today JDK8 foreach and traditional generics in the foreach loop, thought they would be about the same, the result is JDK8 foreach far less generic foreach loop, so why do it?? The test code is very simple
Public static void main (String [] args) {
List List=new ArrayList (a);
for(int i=0; i<100000; I++) {
List. The add (" a ");
}
Long a=System. CurrentTimeMillis ();
For (String action: list) {
If (action==null) {
//do nothing, just judge the
}
}
Long b=System. CurrentTimeMillis ();
System. The out. Println (b - a);//the traditional foreach day 1
List. The forEach (action - & gt; {
If (action==null) {
//do nothing, just judge the
};
});

Long c=System. CurrentTimeMillis ();
System. The out. Println (c - b);//JDK8 time 2
}
Console print time, JDK8 time-consuming traditional foreach dozens of times, strives for the great god

CodePudding user response:

ArrayList don't recommend to use foreach, arrays, and other set can be used, this is I see in "rashly Java",

CodePudding user response:

reference 1st floor can8888 response:
ArrayList don't recommend to use foreach, arrays and other collections can be used, this is I saw in the "local Java",



I'm sorry to have wrong problem,

CodePudding user response:

The forEach and forEach

CodePudding user response:

Try to tell you the test data of expanding the three orders of magnitude

CodePudding user response:

Remember correctly, jdk8 lang mother of expression of performance is not as good as the traditional writing way,
The problems that arise as to why the JDK API and grammar is not d do extreme optimization efficiency,
In theory, object-oriented programming, relative performance oriented process must be reduced, but the maintainability or clarity had the very big promotion,

So a feature used to need not, depends on what you focus on, when the company give you half a year to do function implementation, apparently no one spend 3 months to do performance optimization, more clear and reasonable code is very important at this time, most of the time, mediocre performance problem is not from the algorithm and the API, but from all kinds of system bug,

CodePudding user response:

Here is https://blog.csdn.net/u_ascend/article/details/80777033

CodePudding user response:

Foreach is to optimize the programmer to write code efficiency, rather than to the operational efficiency of the code,
A programming language to consider not only the efficiency of the code, and comprehensive consideration of various factors, such as code efficiency,
Is now popular python, its efficiency is low too outrageous, but it's good to write introduction to efficiency and simplicity in continuously improve his popularity
If you like simple high-performance code,
So assembly and C language is the fastest, the C + + should be able to as you wish

CodePudding user response:

Lambda make code concise, and stream collocation is used, can make full use of multi-core, realize the true parallel, if the data quantity is not big, can only play the role of simplify the code, there is not a prominent performance advantage, even worse than a for loop, because the program for the first time when using lambda need to initialize some lambda framework, these will take time, and for loop don't need these timings, will be able to perform, when the iteration data, lambda performance advantage is reflected

CodePudding user response:

Do you test it in circulation,
Such as sleep (100);
You will find, lambda foreach is multithreading,
Although it is true that the start on slow a lot (after all, running a thread pool), but concurrent processing speed will make the overall results,
Especially in circulation call remote interface, or loop to access the database when there will be special obvious ascension,

CodePudding user response:

references 9 f SarielAngel response:
do you test it inside the loop,
Such as sleep (100);
You will find, lambda foreach is multithreading,
Although it is true that the start on slow a lot (after all, running a thread pool), but concurrent processing speed will make the overall results,
Especially in the loop calls remote interface, or loop to access the database will be particularly evident when ascension,


Tip point, not a list. The forEach ()
But the list. ParallelStream (). The forEach ()

If this is the list. The forEach (), little influence efficiency, just for a change of writing,

CodePudding user response:

10000 data volume foreach slow 6 ms
50000 data foreach fast 3 ms
100000 data foreach fast 32 ms
1 million data foreach fast 93 ms

CodePudding user response:

If you need to multiple classes, the for loop, java8 forEach than the enhanced for loop, high performance, because its design requires preheating

CodePudding user response:

Can't see it that way, these two code exactly the same as the underlying implementation, not JDK8 time-consuming is traditional foreach dozens of times, but more than 100 ms
Your collection again big point, time consuming is more than 100 ms

CodePudding user response:

The size of the amount of data will affect the circulation to adapt
  • Related