1 | sclass PerSecondCounter { |
2 | double interval = 1; // seconds |
3 | long time = sysNow(), count, lastCount, lastDiff; |
4 | |
5 | *() {} |
6 | *(double *interval) {} |
7 | |
8 | void inc() { inc(1); } |
9 | synchronized void inc(int n) { count += n; } |
10 | |
11 | synchronized double getLast() { |
12 | double fps = lastDiff == 0 ? 0 : lastCount*1000.0/lastDiff; |
13 | update(sysNow()-time); |
14 | ret fps; |
15 | } |
16 | |
17 | synchronized double get() { |
18 | long diff = sysNow()-time; |
19 | double fps = (count+lastCount)*1000.0/(diff+lastDiff); |
20 | update(diff); |
21 | ret fps; |
22 | } |
23 | |
24 | void update(long diff) { |
25 | if (diff >= toMS(interval)) { |
26 | lastDiff = diff; |
27 | lastCount = count; |
28 | time = sysNow(); |
29 | count = 0; |
30 | } |
31 | } |
32 | } |
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: | 578 / 1212 |
Version history: | 6 change(s) |
Referenced in: | [show references] |