Libraryless. Click here for Pure Java version (6148L/34K).
1 | sclass MultiBencher { |
2 | settable Runnable action; |
3 | settable double seconds = 10; |
4 | settable double interval = 0.001; |
5 | double elapsedSeconds; |
6 | |
7 | //settable double improvementThreshold = 0.05; |
8 | settable double improvementOverPrintedThreshold = 0.10; |
9 | |
10 | new L<BenchForNSeconds> benchers; |
11 | |
12 | *() {} |
13 | *(Runnable *action) {} |
14 | |
15 | run { |
16 | ensureCapacity(benchers, iceilRatio(seconds, interval)+2); |
17 | long start = nanoTime(), endTime = lround(start+secondsToNanos(seconds)); |
18 | while (nanoTime() < endTime) { |
19 | var bencher = addAndReturn(benchers, new BenchForNSeconds(interval, action)); |
20 | bencher.run(); |
21 | } |
22 | } |
23 | |
24 | long startTime() { ret empty(benchers) ? 0 : first(benchers).startTime; } |
25 | |
26 | long legMS(int i) { |
27 | ret lround(nanosToMilliseconds(benchers.get(i).startTime-startTime())); |
28 | } |
29 | |
30 | S renderLegMS(int i) { |
31 | ret n2(legMS(i)) + " ms"; |
32 | } |
33 | |
34 | S renderLegIndex(int i) { |
35 | ret "Leg " + (i+1) + "/" + l(benchers) |
36 | + " (T+" + renderLegMS(i) + ")"; |
37 | } |
38 | |
39 | toString { |
40 | ret lines(mapWithIndex(benchers, (i, b) |
41 | -> renderLegIndex(i) + ": " + b)); |
42 | } |
43 | |
44 | // "min records" are the times when a new minimum iteration time is reached |
45 | S renderMinRecords() { |
46 | new LS lines; |
47 | long best = 0, iterations = 0, compileTime = 0, compileIterations = 0; |
48 | long printedBest = 0; |
49 | int improvements = 0; |
50 | for i, b over benchers: { |
51 | long time = lround(b.min()); |
52 | iterations += b.iterations(); |
53 | if (i == 0) { |
54 | best = printedBest = time; |
55 | lines.add("First leg: " + n2(time) + " ns"); |
56 | } else { |
57 | //double improvement = doubleRatio(best, time)-1; |
58 | double improvementOverPrinted = doubleRatio(printedBest, time)-1; |
59 | if (time < best) best = time; |
60 | if (/*improvement >= improvementThreshold ||*/ improvementOverPrinted >= improvementOverPrintedThreshold) { |
61 | //printVars(+i, +best, +time, +ratio, +improvement); |
62 | ++improvements; |
63 | S percent = formatDouble1(improvementOverPrinted*100); |
64 | printedBest = time; |
65 | lines.add("Improved " |
66 | + convertToSpacesIf(improvements == 1, "another ") |
67 | + pad(percent, 7) + "% after " |
68 | + pad(renderLegMS(i), 8) |
69 | + pad(appendBracketed(nIterations(iterations)), 22) |
70 | + " - new time: " + pad(b.formatNanos(time), 12)); |
71 | //lines.add("New best time in " + firstToLower(renderLegIndex(i)) + ": " + n2(time) + " ns"); |
72 | compileIterations = iterations; |
73 | compileTime = legMS(i); |
74 | } |
75 | } |
76 | } |
77 | if (!isEmpty()) |
78 | lines.add("\nFully compiled after " + n2(compileTime) + " ms" + appendBracketed(nIterations(compileIterations)) |
79 | + ". Waited for " + formatDouble1(msToSeconds(legMS(l(benchers)-1))) + " s in total"); |
80 | ret lines(lines); |
81 | } |
82 | |
83 | bool isEmpty() { ret empty(benchers); } |
84 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034580 |
Snippet name: | MultiBencher - bench same action in short intervals to see when compilation kicks in |
Eternal ID of this version: | #1034580/40 |
Text MD5: | 799d391bb6ca0c094d314fd30b1edf2c |
Transpilation MD5: | 801e46a29e1c61a2297593734e945803 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-02-19 07:14:01 |
Source code size: | 2939 bytes / 84 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 196 / 411 |
Version history: | 39 change(s) |
Referenced in: | [show references] |