I'm having this problem too and I think I managed to track it down. I started by putting a Game.getUsedCpu() at the top of my main.js and it almost always had a value less than 2, so the reported cpu is actually going into the execution of your scripts.
I have a cpu object in the global scope to keep track of cpu usage and a lot of time goes into the require()
function it seems. I do most of my require
'ing in a module called init, looking like this:
cpu.track("init", function() {
var modules = [
"string", // *must* be first or catch-block will fail
"global",
"math",
"protoGame",
"protoGame",
"protoSpawn",
"protoCreep",
"protoRoom",
"protoSource",
"protoStructure",
"protoRoomPosition"
];
_.each(modules, function(module) {
try {
cpu.begin(module);
require(module)();
cpu.end(module);
}
catch (ex) {
console.log("Exception caught: {0}.Stack: {1}".format(ex, ex.stack));
}
});
_.mixin(Room.prototype, brainRoom, { "chain": false });
if (_.isUndefined(Memory.index)) {
Memory.index = {};
}
});
The output of that looks like this (I stripped away the items that were <2):
```
protoCreep: 10.955753000000001
protoRoom: 6.102737000000001
protoRoom.require: 4.112070000000003
init: 27.029208
protoCreep: 7.1586099999999995
protoRoom: 12.721302000000001
protoRoom.require: 9.670390999999999
init: 28.924640999999998
```
Most of these files modify the prototypes of various game objects(hence the proto prefix) but I also timed the require calls and as you can see they take up most of the time.
To add some more detailed output I decided to time the execution of the modules that are actually being required by adding a begin()/end() call at the top/bottom of the those to time how long it took the VM to evaluate the actual files. The results were as follows:
protoRoom: 5.566376000000002
EnergyMap: 0.04861100000000107
B-Q: 0.0905040000000028
C-Q: 0.08399100000000104
Statistics: 0.04209700000000183
protoRoom.require: 3.4115080000000013
init: 22.651317
It would be really nice if all the source files were actually pre-included before our main functions were executed so that the amount of time spent in require() calls would be reduced to a minimum. Any script file that throws a compile error is ignored. And of course, the scripts shouldn't be executed, just precompiled.