This is not a JIT compiler. Most of these 70 milliseconds were spent on initializing the entire lambda subsystem (the entry point to this logic is probably the LambdaMetaFactory class), and a good bit is also spent on calling the lambda bootstrap (the binding stage, as specified by user fge). Check out this method, just like yours, but with all the individual dimensions (and I'm using nanoTime ):
public static void Iterate(int[] values, int from, int to) { long start = System.nanoTime(); final IntPredicate predicate = i -> i < to && i > from; System.out.println("Predicate lambda creation time:" + NANOSECONDS.toMillis(System.nanoTime() - start)); start = System.nanoTime(); final IntConsumer action = System.out::println; System.out.println("Action lambda creation time:" + NANOSECONDS.toMillis(System.nanoTime() - start)); start = System.nanoTime(); final IntStream stream = IntStream.of(values).filter(predicate); System.out.println("Stream creation time:" + NANOSECONDS.toMillis(System.nanoTime() - start)); start = System.nanoTime(); stream.forEach(action); System.out.println("Stream consumption time:" + NANOSECONDS.toMillis(System.nanoTime() - start)); }
This is what was printed on my machine:
Predicate lambda creation time:53 Action lambda creation time:2 Stream creation time:2 599 Stream consumption time:1 Predicate lambda creation time:0 Action lambda creation time:0 Stream creation time:0 201 ... all timings zero from here on...
You can see that all the overhead of the first call is in the lambda creation part (which includes initialization and binding at the first start), and creating a thread also takes some time. Actual flow consumption takes zero time in all cases.
This effect, of course, is something to keep in mind with the current version of HotSpot: lambda bootstrap is an expensive thing.
Final note: if you change the order of the lambda creation statements, you will see that most of the time is left with the first lambda to be created. This shows us that this is actually just the first general lambda creation that carries most of the initialization cost.
source share