Libraryless. Click here for Pure Java version (7035L/39K).
1 | // function type is A (e.g. String) |
2 | sclass FunctionTimings<A> { |
3 | Map<A, Average> data = syncMap(); |
4 | |
5 | void doCtex aka timeCtex(A function, RunnableWithExceptions r) ctex { |
6 | if (r == null) ret; |
7 | long time = nanoTime(); |
8 | try { |
9 | r.run(); |
10 | } finally { |
11 | time = nanoTime()-time; |
12 | saveTiming(function, time); |
13 | } |
14 | } |
15 | |
16 | <B> B get(A function, IF0<B> f) { |
17 | if (f == null) null; |
18 | new Var<B> var; |
19 | time(function, -> var.set(f!)); |
20 | ret var!; |
21 | } |
22 | |
23 | void do aka time(A function, Runnable r) { |
24 | if (r == null) ret; |
25 | long time = nanoTime(); |
26 | try { |
27 | r.run(); |
28 | } finally { |
29 | time = nanoTime()-time; |
30 | saveTiming(function, time); |
31 | } |
32 | } |
33 | |
34 | void saveTiming(A function, long time) { |
35 | Average avg = syncMapGetOrCreate(data, function, -> new Average); |
36 | avg.add(time); |
37 | } |
38 | |
39 | Map<A, Average> get() { ret cloneMap(data); } |
40 | |
41 | void reset aka clear() { data.clear(); } |
42 | |
43 | // as multi-line string, sorted alphabetically |
44 | public S render aka toString() { |
45 | ret lines(renderedEntries()); |
46 | } |
47 | |
48 | LS renderedEntries() { |
49 | ret ciSorted(map(get(), (f, avg) -> |
50 | functionToString(f) + ": " + n2(iround(nsToMicroseconds(avg!))) |
51 | + " " + microSymbol() + "s (" + n2(iround(avg.n())) + ")")); |
52 | } |
53 | |
54 | S toStringSingleLine() { |
55 | ret joinWithComma(renderedEntries()); |
56 | } |
57 | |
58 | S functionToString(A f) { |
59 | ret firstToUpper(str(f)); |
60 | } |
61 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033714 |
Snippet name: | FunctionTimings |
Eternal ID of this version: | #1033714/15 |
Text MD5: | 7e13d2097f1f504d4967133eba8d51dd |
Transpilation MD5: | 4b95dcd09d17f4965d46d6d9d58a82b0 |
Author: | stefan |
Category: | javax / maths |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-24 15:28:40 |
Source code size: | 1465 bytes / 61 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 179 / 349 |
Version history: | 14 change(s) |
Referenced in: | [show references] |