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

25
LINES

< > BotCompany Repo | #1035210 // timedForNSeconds - better than benchForNSeconds because result and timing are returned (important for "black-holing" the result)

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

Libraryless. Click here for Pure Java version (8006L/45K).

// returns minimal time needed
static <A> Timed<A> timedForNSeconds(double n, S desc default str(f), IF0<A> 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<A>(result, nanosToMS(min));
}

Author comment

Began life as a copy of #1007922

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035210
Snippet name: timedForNSeconds - better than benchForNSeconds because result and timing are returned (important for "black-holing" the result)
Eternal ID of this version: #1035210/10
Text MD5: ed093a85e7ab8dc3f833761a462d4bc3
Transpilation MD5: 024b65fa5fb78192bce8e61258a443f2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-15 07:36:43
Source code size: 875 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 163 / 247
Version history: 9 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1035217 - timedFor5 - better than benchFor5 because result and timing are returned (important for "black-holing" the result)