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

40
LINES

< > BotCompany Repo | #1034576 // BenchForNSeconds

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

Transpiled version (5492L) is out of date.

1  
sclass BenchForNSeconds {
2  
  settable Runnable action;
3  
  settable double seconds; // how many seconds to run the benchmark
4  
  volatile bool done;
5  
  gettable long iterations;
6  
  long startTime, endTime;
7  
  gettable new AverageAndMinMax stats;
8  
  
9  
  *() {}
10  
  *(double *seconds, Runnable *action) {}
11  
  
12  
  run {
13  
    startTime = sysNanos();
14  
    endTime = lround(startTime+secondsToNanos(seconds));
15  
    long time = sysNanos();
16  
    while (time < endTime) {
17  
      action?.run();
18  
      long runTook = sysNanos()-time;
19  
      ++iterations;
20  
      stats.add(runTook);
21  
      ping();
22  
      time = sysNanos();
23  
    }
24  
    endTime = time;
25  
    set done;
26  
  }
27  
  
28  
  S formatNanos(double nanos) { ret n2_round(nanos) + " ns"; }
29  
  
30  
  toString {
31  
    ret stats.isEmpty() ? "Not run"
32  
      : "min/avg/max="
33  
        + n2_round(stats.min()) + "/"
34  
        + n2_round(stats.avg()) + "/"
35  
        + n2_round(stats.max()) + " ns"
36  
        + appendRoundBracketed(nRuns((long) stats.n()));
37  
  }
38  
  
39  
  double min() { ret stats.min(); }
40  
}

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: #1034576
Snippet name: BenchForNSeconds
Eternal ID of this version: #1034576/15
Text MD5: e64aceef74f523a4ca2866e8523fe1e2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-19 06:46:24
Source code size: 1030 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 115 / 228
Version history: 14 change(s)
Referenced in: [show references]