// returns minimal time needed
static Timed timedForNSeconds(double n, S desc default str(f), IF0 f) {
long start = sysNow(), min = -1, print = sysNow();
int seconds = 1;
long count = 0, sum = 0;
A result = null;
while (seconds <= n) {
long time = nanos();
result = f!;
++count;
time = nanos()-time;
min = min < 0 ? time : min(min, time);
sum += time;
double avg = doubleRatio(sum, count);
if (sysNow() >= start+seconds*1000) {
printAndSetConsoleTitleIfMain(
"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 Timed(result, nanosToMS(min));
}