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

80
LINES

< > BotCompany Repo | #1033348 // Count as fast as I can using only 50% of one core

JavaX source code (desktop) [tags: jar-with-dynamicobject use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (7512L/42K).

1  
!7
2  
3  
// Now with processor GHz estimate
4  
5  
set flag LeanMode. set flag OurSyncCollections. set flag collectionMutex_simple. set flag SymbolAsString.
6  
7  
sbool ping() { ret ping_v3(); }
8  
9  
static new PingSource myPingSource;
10  
11  
static double coresToUse = .5;
12  
static double frequency = 100;
13  
static new BoolVar on;
14  
static volatile long result;
15  
static NotTooOften nto = nto(1.0);
16  
17  
sclass Counter {
18  
  new Timestamp start;
19  
20  
  run {
21  
    temp tempSetPingSource(myPingSource);
22  
    loop();
23  
  }
24  
  
25  
  void loop {
26  
    long i = 0;
27  
    var ping = pingSource();
28  
    
29  
    while (true) {
30  
      if (ping_trueIfSlept(ping)) {
31  
        result = i;
32  
        if (nto!) printStats();
33  
      }
34  
      
35  
      ++i;
36  
    }
37  
  }
38  
39  
  void printStats {
40  
    Seconds cpu = new(nanosToSeconds(currentThreadTotalCPUNanos()));
41  
    Seconds clock = new(elapsedSeconds(start));
42  
    double cpuRatio = doubleRatio(cpu, clock);
43  
    double numbersPerSecond = doubleRatio(result, clock!);
44  
    double npsPerCore = doubleRatio(numbersPerSecond, cpuRatio);
45  
    double nsPerNumber = doubleRatio(oneBillion(), npsPerCore);
46  
    double assumedCycles = 2;
47  
    Freq speedEstimate = new(assumedCycles*npsPerCore);
48  
    
49  
    print("I have counted until " + n2(result) + ". pid=" + pid() + ", cpu time: " + cpu + " (clock: " + clock
50  
      + ", ratio: " + formatDouble1(cpuRatio*100) + "%) - "
51  
      + n2(lfloor(numbersPerSecond)) + " numbers per second = "
52  
      + n2(lfloor(npsPerCore)) + " numbers per second per core = "
53  
      + formatDouble1(nsPerNumber) + " ns per number"
54  
      + " => Processor speed estimate: " + speedEstimate.format1());
55  
  }
56  
}
57  
58  
p {
59  
  new Schedule<Runnable> schedule;
60  
  //enableScaffolding(schedule);
61  
  schedule.add(0, r {
62  
    dontprint("on");
63  
    myPingSource.action(null);
64  
    on.set();
65  
  });
66  
  schedule.add(coresToUse, r {
67  
    dontprint("off");
68  
    on.unset();
69  
    myPingSource.action(-> {
70  
      temp tempPingPrivileged();
71  
      on.waitUntilTrue();
72  
      true;
73  
    });
74  
  });
75  
  schedule.jumpFromTo(1, 0);
76  
  schedule.speedUp(frequency);
77  
  //printStruct(schedule);
78  
  thread "Counting" { new Counter().run(); }
79  
  schedule.run();
80  
}

Author comment

Began life as a copy of #1033318

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): bhatertpkbcr, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033348
Snippet name: Count as fast as I can using only 50% of one core
Eternal ID of this version: #1033348/15
Text MD5: 905075fa03ff503bb63bcd03651124af
Transpilation MD5: f74276c187b6f2ec59953af04162bc25
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-25 02:52:49
Source code size: 2159 bytes / 80 lines
Pitched / IR pitched: No / No
Views / Downloads: 188 / 640
Version history: 14 change(s)
Referenced in: [show references]