When profiling execution time of java code, the common approach is to utilise the following approach:
long start = System.nanoTime();
expensiveMeothd(array);
long runtime = System.nanoTime() - start;
Unfortunately this approach is not applicable in the confines of Java8 stream syntax.
What is the best practice for measuring a stream method execution time?
For example if I wanted to calculate how long:
.map(x -> someexpensiveMeothod(x))
takes to execute.
One approach I have considered is wrapping the stream return in an object that stores the nanotime. However this approach seems less than ideal. Here is my concept:
public class Profiler {
public List list;
public long startTime;
public long endTime;
public Profiler(List list, long s, long e) {
this.list = list;
this.startTime = s;
this.endTime = e;
}
}
List<Profiler> results = myList.stream()
.map(x -> new Profiler(x, System.nanoTime(), null))
.map(x -> {
expensiveMethod(x.list);
x.endTime = System.nanoTime();
return x;
})
.collect(Collectors.toList());
Additionally, is it actually appropriate to measure the execution time of individual stream method calls on principle?