Skip to content

Instantly share code, notes, and snippets.

@gcoop
Created May 4, 2011 09:04
Show Gist options
  • Select an option

  • Save gcoop/954962 to your computer and use it in GitHub Desktop.

Select an option

Save gcoop/954962 to your computer and use it in GitHub Desktop.

Revisions

  1. gcoop created this gist May 4, 2011.
    44 changes: 44 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    /**
    * Simple benchmark class in Titanium.
    *
    * @usage
    * Benchmark.start();
    * Benchmark.addMarker("Did some cool stuff.");
    * Benchmark.end(); // Print that shit out.
    *
    * @author Gavin Cooper.
    */
    Benchmark = (function () {
    var markers = [];

    return {
    start: function () {
    markers = [];

    markers.push({title: "Start", value: new Date()});
    },
    addMarker: function (title) {
    if (markers.length <= 0) {
    Benchmark.start();
    }

    markers.push({title: title, value: new Date()});
    },
    end: function () {
    markers.push({title: "End", value: new Date()});

    Ti.API.debug("==== BENCHMARK START ====");
    for (var i = 1; i < markers.length; i++) {
    var duration = markers[i].value.getTime() - markers[i - 1].value.getTime();
    Ti.API.debug("==== "+markers[i].title+" - "+duration+" m/s");

    if (i == (markers.length - 1)) {
    Ti.API.debug("==== Total Time: "+(markers[i].value.getTime() - markers[0].value.getTime())+" m/s");
    }
    }
    Ti.API.debug("==== BENCHMARK END ====");

    markers = [];
    }
    };
    })();