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).

1  
// returns minimal time needed
2  
static <A> Timed<A> timedForNSeconds(double n, S desc default str(f), IF0<A> f) {
3  
  long start = sysNow(), min = -1, print = sysNow();
4  
  int seconds = 1;
5  
  long count = 0, sum = 0;
6  
  A result = null;
7  
  while (seconds <= n) {
8  
    long time = nanos();
9  
    result = f!;
10  
    ++count;
11  
    time = nanos()-time;
12  
    min = min < 0 ? time : min(min, time);
13  
    sum += time;
14  
    double avg = doubleRatio(sum, count);
15  
    if (sysNow() >= start+seconds*1000) {
16  
      printAndSetConsoleTitleIfMain(
17  
        "avg=" + formatDouble(nanosToMS(avg), 3) + " ms, " +
18  
        "min=" + formatDouble(nanosToMS(min), 3) + " ms: " +
19  
        desc + " (" + seconds + "/" + n + " s, last=" + formatDouble(nanosToMS(time), 3) + " ms, " + n2(count) + "/s)");
20  
      ++seconds;
21  
      count = sum = 0;
22  
    }
23  
  }
24  
  ret Timed<A>(result, nanosToMS(min));
25  
}

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: 162 / 246
Version history: 9 change(s)
Referenced in: [show references]