Fix require times.
-
I've been doing a lot of work to bring down my CPU usage, but I can't seem to catch a break from the time it takes to load up my script. I've been getting a wide range of CPU limit notifications (anywhere from 0 to thousands of notifications per hour).
After putting in a ton of time optimizing my script, and working with my profiler, I don't know that there is anything I can do.
Below is my current main.js.
require('perf');
require('creep');
require('source');
require('spawns');
require('structures');
require('room-position');
var profiler = require('profiler');
profiler.enable();
module.exports.loop = function() {
profiler.wrap(function() {
var start = Game.getUsedCpu();
var index = 0;
for (var roomName in Game.rooms) {
index++;
if (index === 1 || Game.cpuLimit > 60) {
Game.rooms[roomName].work();
}
}
console.log(Game.getUsedCpu() - start, start);
});
}
-
If you type `console.log('nocache')` right before the `module.exports.loop` line,
you'll see, that cache is not permamemt.
It means, that the code outside of `module.exports.loop` method (including all your `require()` calls) is executed occasionly.
As far as each `require()` call is *CPU-heavy*, you do have that spikes at start sometimes.In the presence of cpu spikes, you may want to get know the real 'used cpu' value, that can be compared to your 'cpu limit'. So, you may calculate script's execution time average, let us say: for the last 30 ticks. At least it works fine for me.