I've been trying to figure out if there's a way to write a method that takes a Java 8 method reference as an input,runs that method and returns the time it took to run.
public static long time(Runnable c) {
long start, end;
start = System.currentTimeMillis();
c.run();
end = System.currentTimeMillis();
return (end - start);
}
This is what I have so far, but this will only work for a method with no parameters...I'm looking for something that can work with a method that can take parameters as well. Is that possible?
Well, a work around is to wrap your method inside a lambda
int foo =...
Object bar = ...
long elapsed = time( () -> myMethod(foo, bar));