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

32
LINES

< > BotCompany Repo | #1006530 // PerSecondCounter - thread-safe

JavaX fragment (include)

sclass PerSecondCounter {
  double interval = 1; // seconds
  long time = sysNow(), count, lastCount, lastDiff;
  
  *() {}
  *(double *interval) {}
  
  void inc() { inc(1); }
  synchronized void inc(int n) { count += n; }
  
  synchronized double getLast() {
    double fps = lastDiff == 0 ? 0 : lastCount*1000.0/lastDiff;
    update(sysNow()-time);
    ret fps;
  }
  
  synchronized double get() {
    long diff = sysNow()-time;
    double fps = (count+lastCount)*1000.0/(diff+lastDiff);
    update(diff);
    ret fps;
  }
  
  void update(long diff) {
    if (diff >= toMS(interval)) {
      lastDiff = diff;
      lastCount = count;
      time = sysNow();
      count = 0;
    }
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1006530
Snippet name: PerSecondCounter - thread-safe
Eternal ID of this version: #1006530/7
Text MD5: fa8c75a4cc9b7ab6af6b78c7ac9d38f6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-06 23:21:06
Source code size: 721 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 577 / 1210
Version history: 6 change(s)
Referenced in: [show references]