Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which would be better in terms of performance Lambda or simple loop? [duplicate]

I have quickly read over the Oracle Lambda Expression documentation.

This kind of example has helped me to understand better, though:

//Old way: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); for(Integer n: list) { System.out.println(n); }  //New way: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); list.forEach(n -> System.out.println(n));   //or we can use :: double colon operator in Java 8 list.forEach(System.out::println); 

Still, I don't understand why it's such an innovation. It's just a method that dies when the "method variable" ends, right? Why should I use this instead of a real method? Which would be better option in terms of performance. Lambda or simple loop.

like image 671
Ankur Mahajan Avatar asked Jul 28 '15 11:07

Ankur Mahajan


People also ask

Does lambda expression improve performance?

Oracle claims that use of lambda expressions also improve the collection libraries making it easier to iterate through, filter, and extract data from a collection. In addition, new concurrency features improve performance in multicore environments.

Which is faster for loop or stream?

Again, the for- loop is faster that the sequential stream operation, but the difference on an ArrayList is not nearly as significant as it was on an array.

Why are streams better than for loops?

Advantages of the streams:Your stream-handling code doesn't need to know the source of the stream or its eventual terminating method. Streams can succinctly express quite sophisticated behavior. Streams can be a replacement for looping because they allow for the processing of a sequence of data (similarly to a loop).


2 Answers

My advice would be:

  1. Use the style that you and your coworkers agree is most maintainable.

  2. If you and your colleagues are not yet comfortable with lambdas, keep learning.

  3. 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.

like image 86
Stephen C Avatar answered Oct 15 '22 01:10

Stephen C


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.

like image 32
Tagir Valeev Avatar answered Oct 15 '22 01:10

Tagir Valeev