Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

30
LINES

< > BotCompany Repo | #1007922 // benchForNSeconds

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (2749L) is out of date.

// returns result of function
static <A> A benchForNSeconds(double n, S desc default str(f), IF0<A> 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;
}

Author comment

Began life as a copy of #1006403

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1007922
Snippet name: benchForNSeconds
Eternal ID of this version: #1007922/10
Text MD5: b65e8c8c550fcd414aa3024263f78425
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-16 21:54:53
Source code size: 994 bytes / 30 lines
Pitched / IR pitched: No / No
Views / Downloads: 590 / 757
Version history: 9 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1009317 - benchForNSeconds2 [dev.]
#1034576 - BenchForNSeconds
#1035210 - timedForNSeconds - better than benchForNSeconds because result and timing are returned (important for "black-holing" the result)