Need help with this code for performance issue



  • I write a little script, like the following

    Creep.prototype.transferEnergyAndMove = function(mysource) {

    if(this.transferEnergy(mysource) == ERR_NOT_IN_RANGE) {
        var startCpu = Game.getUsedCpu();     
    
    this.moveTo(mysource,{reusePath:9,serializeMemory:true});
    
    var elapsed = Game.getUsedCpu() - startCpu;
    console.log(' transferEnergyAndMove has used '+elapsed+' CPU time');   
    

    }

    };

    and the results shock to me ,
    [10:13:57] transferEnergyAndMove has used 0.30031800000000075 CPU time
    [10:13:59] transferEnergyAndMove has used 0.3367020000000007 CPU time
    [10:14:02] transferEnergyAndMove has used 0.323207 CPU time
    [10:14:05] transferEnergyAndMove has used 0.3165210000000016 CPU time
    [10:14:07] transferEnergyAndMove has used 13.078899999999999 CPU time
    [10:14:10] transferEnergyAndMove has used 0.3080759999999998 CPU time
    [10:14:10] transferEnergyAndMove has used 1.7853649999999988 CPU time

    What the terrible CPU time use like that 'transferEnergyAndMove has used 13.078899999999999 CPU time', how to do reduce this!? Any good methods to replace the moveTo() !?



  • Your only options would probably be to implement path caching or write your own pathing function. I've found caching to be effective most of the time.



  • I'm sure you've figured this out by now, but moving takes a static .2 CPU time (the .3 is a little concerning).  The larger 13 is probably due to pathfinding.  As the previous poster said you could either roll your own path finder (although I believe it is faster now), or cache the results somehow.