diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-01-15 17:30:00 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-01-15 17:30:00 -0800 |
commit | 8e432f90294db453f104a253fbc4df8b6ec83920 (patch) | |
tree | e092101f1a4ed4645d98d25f04d4531c62c8e26c | |
parent | c9799044984c5200d84bce9e5902250e92ed43ed (diff) |
add js benchmark utility
-rw-r--r-- | src/utility.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/utility.js b/src/utility.js index e49c8509..5c934483 100644 --- a/src/utility.js +++ b/src/utility.js @@ -306,3 +306,22 @@ function log2(x) { return Math.log(x)/Math.LN2; } +function Benchmarker() { + var starts = {}, times = {}, counts = {}; + this.start = function(id) { + //printErr(['+', id, starts[id]]); + starts[id] = (starts[id] || []).concat([Date.now()]); + }; + this.end = function(id) { + //printErr(['-', id, starts[id]]); + assert(starts[id], new Error().stack); + times[id] = (times[id] || 0) + Date.now() - starts[id].pop(); + counts[id] = (counts[id] || 0) + 1; + }; + this.print = function() { + var ids = keys(times); + ids.sort(function(a, b) { return times[b] - times[a] }); + printErr('times: \n' + ids.map(function(id) { return id + ' : ' + counts[id] + ' times, ' + times[id] + ' ms' }).join('\n')); + }; +}; + |