I've been demo testing a bit with lambda performance using Java8 VS. Java8 public functions.
The case is as the following:
I have a list existing of 10 people (5 male and 5 female).
I'd like to know which woman have an age between 18 and 25
Now, when I'm executing these steps a milion times, the results would be:
Lambda with ForEach took: 395 ms (396 ms using JUnit)
Public functions took: 173 ms (169 ms using JUnit)
Lambda with Collect took: 334 ms (335 ms using JUnit)
Now I didn't expect the execution time of lambda to be twice up to six times longer then regular functions.
So, now I'm pretty much wondering whether I've missed something here.
The source can be found here: pastebin.com/BJBk4Tu6
Follow up:
- When expanding the list to 1.000.000 items
- and filtering all young adult woman once
The results would be:
Lambda with ForEach took: 59 ms
Public functions took: 15 ms
Lambda with Collect took: 12 ms
However, when I'm trying to filter the same list existing of 1.000.000 people 100 times, the results would be:
Lambda with ForEach took: 227 ms
Public functions took: 134 ms
Lambda with Collect took: 172 ms
So, as a final conclusion: Lambdas are quicker when it comes to filtering larger lists while public function (the old way) are quicker at filtering smaller lists.
Also, public functions are quicker when it comes to filtering any lists multiple times, for whatever purpose you'd require to do that.
Latest code: pastebin.com/LcVhgnYv
As pointed out in the comments: You can hardly draw any conclusion from such a single, simple and isolated microbenchmark run.
Partially quoting from another (otherwise unrelated) answer:
I applied these basic steps to your program. Here is an MCVE :
The output on My Machine® is along the lines of this:
You can see that the difference between the
ForEach
and thedefault
(public methods) approach is vanishing even for smaller lists. For larger lists, there even seems to be a slight advantage for the lambda-based approaches.To emphasize this again: This is a very simple microbenchmark, and even this does not necessarily tell much about the performance of these approaches in practice. However, it is at least reasonable to assume that the difference between the
ForEach
and the public methods is not as large as suggested by your initial test. Nevertleless: +1 from me for anybody who runs this in JMH or Caliper and posts some further insights about this.