// returns result of function
static A benchForNSeconds(double n, S desc default str(f), IF0 f) {
benchForNSeconds(r { f! }, n);
ret f!;
}
// returns minimal time needed
static long benchForNSeconds(S desc default str(r), Runnable r, double n) {
long start = sysNow(), min = -1, print = sysNow();
int seconds = 1;
long count = 0, sum = 0;
while (seconds <= n) {
long time = nanos();
r.run();
++count;
time = nanos()-time;
min = min < 0 ? time : min(min, time);
sum += time;
double avg = doubleRatio(sum, count);
if (sysNow() >= start+seconds*1000) {
printAndSetConsoleTitleIfMain(
"n=" + n2(count) + ", avg=" + formatDouble(nanosToMS(avg), 3) + " ms, " +
"min=" + formatDouble(nanosToMS(min), 3) + " ms: " +
desc + " (" + seconds + "/" + n + " s, last=" + formatDouble(nanosToMS(time), 3) + " ms, " + n2(count) + "/s)");
++seconds;
count = sum = 0;
}
}
ret min;
}