Java Reference
In-Depth Information
Table 12-9. Time to execute the calc() method in a new classloader
Implementation
Time in microseconds
Anonymous classes 267
Lambdas
181
One point about these numbers: they have been measured after a suitable warm-up period to
allow for compilation. But one other thing happens during the warm-up period: the class files
are read from disk for the first time. The OS will keep those files in memory (in the OS
filesystem buffers). So the very first execution of the code will take much longer, as the calls
to the OS to read the files will actually load them from disk. Subsequent calls will be
faster—they will still call the OS to read the files, but since the files are in the OS memory,
their data is returned quickly. So the result for the anonymous class implementation is better
than it might otherwise be, since it doesn't include any actual disk reading of the class files.
QUICK SUMMARY
1. The choice between using a lambda or an anonymous class should be dictated by
ease of programming, since there is no difference between their performance.
2. Lambdas are not implemented as classes, so one exception to that rule is in envir-
onments where classloading behavior is important; lambdas will be slightly faster
in that case.
Stream and Filter Performance
One other key feature of Java 8, and one that is frequently used in conjunction with lambdas,
is the new Stream facility. One very important performance feature of streams is that they
can automatically parallelize code. Information about parallel streams can be found in
Chapter 9 ; this section discusses some general performance features of streams and filters.
Search WWH ::




Custom Search