Which would be better in terms of performance Lambda or simple loop? [duplicate]
My advice would be:
-
Use the style that you and your coworkers agree is most maintainable.
-
If you and your colleagues are not yet comfortable with lambdas, keep learning.
-
Don't obsess over performance. It is often not the most important thing.
Generally speaking, lambdas and streams provide a more concise and (once everyone is up to speed) more readable way of expressing this kind of algorithm. Performance is not the primary goal.
If performance does become an issue, then the standard advice is to code, test, benchmark, profile and optimize. And do it in that order! You can easily waste a lot time by optimizing at the coding stage, or by optimizing code that has minimal impact on overall application performance.
- Let the application benchmarks tell you if you need to optimize at all.
- Let the profiler point out the parts of your code that are worthy of the effort of optimization.
In this specific example, the performance difference is going to be too small to measure. And if you scaled up to a list of millions of elements, the performance will be dominated by the time taken to build the list and write the numbers. The different ways of iteration will only contribute a small part to the overall performance.
And for folks, who (despite all of the above) still want to know whether it is faster to use a lambda or a conventional loop, the best general answer is:
"It depends on all sorts of factors that 1) are not well understood, and 2) liable to change as Java compiler technology evolves.
We could give you an answer for a specific example with a specific Java major/minor/patch release, but it would be unwise to generalize.
Why should I use this instead of a real method?
You should not. Use the approach which you like more.
As for performance, I guess, all these versions are roughly equally fast. Here I/O operation (println
) is much slower than all possible overhead of calling lambda or creating an iterator. In general forEach
might be slightly faster as it does everything inside the single method without creating the Iterator
and calling hasNext
and next
(which is implicitly done by for-each loop). Anyway, this depends on many factors, such as how often you call this code, how long your list is, whether JIT compiler managed to devirtualize the iterator and/or lambda, and so on.
It allows you write in one line (while having enough readability) something, what was not possible before. Performance is not issue here O(n)
stays O(n)
.
For example in my Windows Phone App, I have sessions and in that sessions are performers and I want to select all sessions which have one concrete performer (like you want to see all the movies some actor plays in). In Java 1.7 or less I had to create loop with inner loop, checking for it, returning null if there is no performer etc. And with lambda expressions, I can do this :
//performerId is parameter passed by method before
Sessions.Where(x => x.Performers.Where(y => y.PerformerId == performerId).FirstOrDefault() != null)
It is same in Java now (however I am not working on 1.8 project right now, I do not have Java example, but I am looking forward to it).