I am trying to simply time a function by using the Runnable interface to wrap around whichever function I need.
private static double time(Runnable runnable) { // returns how long runnable took
long startTime = System.nanoTime();
runnable.run();
return (System.nanoTime() - startTime) / 1000000000.0;
}
then I could simply do the following:
double durationOfFunctionA = time(Driver::functionA); // functionA belongs in class Driver
however, if I have a function that takes a parameter, it must be modified to:
double durationOfFunctionB = time(() -> functionB(someParameter));
The problem I am having is that 'someParameter' must be final or effectively final. Is there any workaround this problem? I have seen loops with forEach, but I need this parameter to be exponential from 1, 10, 100 -> until a condition is met. The code is this:
public static void main(String[] args) {
double timer = 0;
int size = 1;
while(timer <= 10) {
timer = time(() -> functionB(size));
size *= 10;
}
}
I require functionB to take in a parameter because I want to test the complexity/big-O of it. I am worried that I am not coding/using lambda expressions the correct way. If someone can help solve this problem or find another solution, that would be appreciated.
As a side note, I do know that I don't have to make this so complex with a Runnable interface, I can just do the timing right in the while loop. However, I just wanted to see if it were possible to do such a thing both so I could just input some function to test, and as syntactic sugar.