How much CPU does cycle eats.



  • I have a cycle that counts how much energy are in the spawn and in the extensions. Does he cost much CPU or not.



  • It really depends on what you're doing and how you're doing it and without seeing any code it is really difficult to determine if your code is using up a lot of CPU or not. I wrote a cpu module for this purpose, of which I've attached the code below. Put this in a file named cpu.js and in the top of your main put global.cpu = require("cpu"); immediately followed by a cpu.init();

    "use strict";
    
    var _ = require("lodash");
    
    function init() {
        Memory.cpu = {};
    }
    
    function begin(key) {
        if (Memory.cpu[key] === undefined) {
            Memory.cpu[key] = {
                total: 0
            };
        }
    
        if (Memory.cpu[key].start === undefined)
            Memory.cpu[key].start = Game.getUsedCpu();
    }
    
    function end(key) {
        if (Memory.cpu[key] === undefined) {
            console.log("[WARN] Ending logging for '{0}' without begin()".format(key));
    
            if (Memory.cpu[key] === undefined) {
                Memory.cpu[key] = {
                    total: 0,
                    start: 0
                };
            }
        }
    
        Memory.cpu[key].total += Game.getUsedCpu() - Memory.cpu[key].start;
        delete Memory.cpu[key].start;
    }
    
    function track(key, fn) {
        if (key === undefined)
            throw new Error("cpu.track(key, function) was called without a key and function.");
        if (!_.isFunction(fn))
            throw new Error("cpu.track(key, function) was called without a function.");
    
        if (Memory.cpu === undefined)
            Memory.cpu = {};
    
        var begin = Game.getUsedCpu();
        fn();
        var total = Game.getUsedCpu() - begin;
    
        if (Memory.cpu[key] === undefined) {
            Memory.cpu[key] = {
                total: total
            };
        } else {
            Memory.cpu[key].total += total;
        }
    }
    
    function show(keys) {
        if (Memory.cpu === undefined)
            return;
    
        var text = "CPU usage:\n";
    
        if (Memory.cpu === undefined)
            return;
    
        _.each(Memory.cpu, function(value, key) {
            if (keys === undefined || _.contains(keys, key))
                text += "{0}: {1}\n".format(key, value.total);
        });
        text += "Total CPU used: {0}".format(Game.getUsedCpu());
        console.log(text);
    }
    
    module.exports = {
        init: init,
        begin: begin,
        end: end,
        track: track,
        show: show
    };
    

    There are two ways in which you can use this module: You can either call cpu.begin("nameToTrack"); execute your code and then call cpu.end("nameToTrack");. Or you can wrap whatever your wish to time into a parameterless function and call the track(key, fn) function like so:

    cpu.track("someName", function() {
        var totalEnergy = _.reduce(Game.structures, function(total, s){
             return total + s.energy;
        },0);
    });
    

    To display the timing results you can use cpu.show(); or cpu.show(["keyOne", "keyTwo"]);. The second version will only display the keys passed into the array. I have a version of my cpu module that tracks averages over several ticks but I'm sure you can figure that out yourself.



  • Thanks toolmaker, this helps a lot.. Still I really have weird results in term of CPU usage for simple harvesters... 100 cycles is so low!