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).

!7

// Now with processor GHz estimate

set flag LeanMode. set flag OurSyncCollections. set flag collectionMutex_simple. set flag SymbolAsString.

sbool ping() { ret ping_v3(); }

static new PingSource myPingSource;

static double coresToUse = .5;
static double frequency = 100;
static new BoolVar on;
static volatile long result;
static NotTooOften nto = nto(1.0);

sclass Counter {
  new Timestamp start;

  run {
    temp tempSetPingSource(myPingSource);
    loop();
  }
  
  void loop {
    long i = 0;
    var ping = pingSource();
    
    while (true) {
      if (ping_trueIfSlept(ping)) {
        result = i;
        if (nto!) printStats();
      }
      
      ++i;
    }
  }

  void printStats {
    Seconds cpu = new(nanosToSeconds(currentThreadTotalCPUNanos()));
    Seconds clock = new(elapsedSeconds(start));
    double cpuRatio = doubleRatio(cpu, clock);
    double numbersPerSecond = doubleRatio(result, clock!);
    double npsPerCore = doubleRatio(numbersPerSecond, cpuRatio);
    double nsPerNumber = doubleRatio(oneBillion(), npsPerCore);
    double assumedCycles = 2;
    Freq speedEstimate = new(assumedCycles*npsPerCore);
    
    print("I have counted until " + n2(result) + ". pid=" + pid() + ", cpu time: " + cpu + " (clock: " + clock
      + ", ratio: " + formatDouble1(cpuRatio*100) + "%) - "
      + n2(lfloor(numbersPerSecond)) + " numbers per second = "
      + n2(lfloor(npsPerCore)) + " numbers per second per core = "
      + formatDouble1(nsPerNumber) + " ns per number"
      + " => Processor speed estimate: " + speedEstimate.format1());
  }
}

p {
  new Schedule<Runnable> schedule;
  //enableScaffolding(schedule);
  schedule.add(0, r {
    dontprint("on");
    myPingSource.action(null);
    on.set();
  });
  schedule.add(coresToUse, r {
    dontprint("off");
    on.unset();
    myPingSource.action(-> {
      temp tempPingPrivileged();
      on.waitUntilTrue();
      true;
    });
  });
  schedule.jumpFromTo(1, 0);
  schedule.speedUp(frequency);
  //printStruct(schedule);
  thread "Counting" { new Counter().run(); }
  schedule.run();
}

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: 171 / 610
Version history: 14 change(s)
Referenced in: [show references]