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

61
LINES

< > BotCompany Repo | #1033714 // FunctionTimings

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

Libraryless. Click here for Pure Java version (7035L/39K).

// function type is A (e.g. String)
sclass FunctionTimings<A> {
  Map<A, Average> data = syncMap();
  
  void doCtex aka timeCtex(A function, RunnableWithExceptions r) ctex {
    if (r == null) ret;
    long time = nanoTime();
    try {
      r.run();
    } finally {
      time = nanoTime()-time;
      saveTiming(function, time);
    }
  }
  
  <B> B get(A function, IF0<B> f) {
    if (f == null) null;
    new Var<B> var;
    time(function, -> var.set(f!));
    ret var!;
  }

  void do aka time(A function, Runnable r) {
    if (r == null) ret;
    long time = nanoTime();
    try {
      r.run();
    } finally {
      time = nanoTime()-time;
      saveTiming(function, time);
    }
  }

  void saveTiming(A function, long time) {
    Average avg = syncMapGetOrCreate(data, function, -> new Average);
    avg.add(time);
  }
  
  Map<A, Average> get() { ret cloneMap(data); }
  
  void reset aka clear() { data.clear(); }
  
  // as multi-line string, sorted alphabetically
  public S render aka toString() {
    ret lines(renderedEntries());
  }
  
  LS renderedEntries() {
    ret ciSorted(map(get(), (f, avg) ->
      functionToString(f) + ": " + n2(iround(nsToMicroseconds(avg!)))
        + " " + microSymbol() + "s (" + n2(iround(avg.n())) + ")"));
  }
  
  S toStringSingleLine() {
    ret joinWithComma(renderedEntries());
  }
  
  S functionToString(A f) {
    ret firstToUpper(str(f));
  }
}

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: 178 / 347
Version history: 14 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)