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

25
LINES

< > BotCompany Repo | #1009317 // benchForNSeconds2 [dev.]

JavaX fragment (include)

1  
// returns result of function
2  
static <A> A benchForNSeconds2(double n, S desc default str(f), IF0<A> f) {
3  
  benchForNSeconds2(n, r { f! });
4  
  ret f!;
5  
}
6  
7  
// returns minimal time needed
8  
// TODO: sub-second duration
9  
static long benchForNSeconds2(S desc, Runnable r, double n, int innerRepetitions) {
10  
  long start = sysNow(), min = -1, print = sysNow();
11  
  int seconds = 1;
12  
  while (seconds <= n) {
13  
    long time = sysNow();
14  
    for (int i = 0; i < innerRepetitions; i++)
15  
      r.run();
16  
    long now = sysNow();
17  
    time = now-time;
18  
    min = min < 0 ? time : min(min, time);
19  
    if (now >= start+seconds*1000) {
20  
      printAndSetConsoleTitle(min + " ms: " + desc + " (" + seconds + "/" + (n*(long) innerRepetitions) + " s, last=" + time + " ms)");
21  
      ++seconds;
22  
    }
23  
  }
24  
  ret min;
25  
}

Author comment

Began life as a copy of #1007922

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1009317
Snippet name: benchForNSeconds2 [dev.]
Eternal ID of this version: #1009317/4
Text MD5: af307d490a9f0ce4aed121ca44004e87
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-24 19:51:56
Source code size: 807 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 525 / 557
Version history: 3 change(s)
Referenced in: [show references]