Initial CPU usage



  • My first line of the main module is:
    console.log(Game.getUsedCpu());

    I would expect this number to be 0, but it almost never is, and often is higher then 50! Is it expected?



  • A lot has been talked about this, you should search for similar topics on the forum. Basically the initialization phase takes some time, and although the Game scope is not built from scratch every tick, still a slight portion of your CPU is consumed for that phase. It basically depends on how many objects (creeps,structures,flags,memory...) the game needs to initialize for you. And since you are the player with probably the most game objects, this phase consumes quite some time for you.

    For me, who only operates of few of my rooms simultaneously, this value is < 3CPU 95% of the time with occasional spikes near the 50 mark (I guess that is the case, when the global object gets reinitialized).



  • I think some efforts should be made to help us there. I only have 4 rooms and reach the 100 CPU limit too often, trying to optimize most of my code, but it is very hard to figure out what costs much.



  • DilGaladh, If you have a look at the documentation, every API method has its CPU costs stated in the top right corner (e.g. http://support.screeps.com/hc/en-us/articles/203079011-Room#findPath has high costs) . As a rule of thumb, you can say every pathfinding call is very expensive. Finding objects is quite costly as well, so you should minimize them and cache your results.



  • The problem chris is that currently randomly my init cost goes to 42 cpu (like DarkTrooper7 says). How then am I supposed to be able to handle a memory that uses uniform cpu against ticks? It's a real nightmare.



  • This initial value of the used CPU does not have anything to do with the Game object creation (which is never taken into account in players' CPU calculation), but with the main module parsing. Once in several tens ticks, when runtime servers reset their condition, there is a necessity of repeated compiling of all your modules. While required extra modules are compiled only upon the require call, the main module is compiled instantly after the script run. If it has a lot of code, the delay for compilation can also be rather big.

    To verify this, you can move all the main module contents into another module, for example main2, and do the following from main:

    console.log(Game.getUsedCpu());
    require('main2');
    

    In this case, the displayed CPU should always be close to zero.