Libraryless. Click here for Pure Java version (278L/3K/8K).
1 | !752 |
2 | |
3 | p { |
4 | thread "Burn 10%" { |
5 | while licensed { |
6 | limitCPU(10); |
7 | } |
8 | } |
9 | |
10 | while licensed { |
11 | sleepSeconds(1); |
12 | print(doze_sleepTime + " / " + doze_calcTime |
13 | + " = " + formatDouble(((double) doze_calcTime)/doze_sleepTime, |
14 | 2)); |
15 | } |
16 | } |
17 | |
18 | static int doze_granularity = 10; |
19 | static volatile long doze_calcingSince; |
20 | static volatile long doze_sleepTime, doze_calcTime; |
21 | |
22 | svoid limitCPU(double percentage) { |
23 | ping(); |
24 | if (doze_calcingSince == 0) { // first time |
25 | doze_calcingSince = now(); |
26 | ret; |
27 | } |
28 | long newCalcTime = now() - doze_calcingSince; |
29 | if (newCalcTime >= doze_granularity) { // don't try to sleep too often |
30 | doze_calcTime += newCalcTime; |
31 | double ratio = percentage/100; |
32 | double allowedCalcTime = doze_sleepTime*ratio/(1-ratio); |
33 | long sleep = toLong(max(0, doze_calcTime-allowedCalcTime)); |
34 | if (sleep > 0) { |
35 | sleep(sleep); |
36 | doze_sleepTime += sleep; |
37 | } |
38 | doze_calcingSince = now(); |
39 | } |
40 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1004536 |
Snippet name: | Test limitCPU (CPU limiting per thread) |
Eternal ID of this version: | #1004536/1 |
Text MD5: | 3c24b7f174a9d97b18cdb99b060015cd |
Transpilation MD5: | 4df863a29e4eef61edc9c63ec583500e |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-08-20 14:41:37 |
Source code size: | 997 bytes / 40 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 605 / 686 |
Referenced in: | [show references] |