Skip to content

Instantly share code, notes, and snippets.

@futjikato
Created April 7, 2015 11:05
Show Gist options
  • Save futjikato/3560b34de20046fd687a to your computer and use it in GitHub Desktop.
Save futjikato/3560b34de20046fd687a to your computer and use it in GitHub Desktop.
/**
* Measures the runtime for a given function.
*
* fn {function} Algorithm function to be measured. First parameter is the `done` callback that should be called on termination.
* iterations {int} How often the function should be called
* res {function} Function called with the resulting runtime in ms.
*
* Example
* ```javascript
* measureRt(function(done) {
* setTimeout(done, 1)
* }, 10, function(rt) {
* console.log('runtime:', rt);
* });
* ```
*/
function measureRt(fn, iterations, res) {
for(var i = 0 ; i < iterations ; i++) {
var startDate = new Date(),
startTs = startDate.getTime(),
startMs = startDate.getMilliseconds();
fn(function() {
var endDate = new Date(),
endTs = endDate.getTime(),
endMs = endDate.getMilliseconds();
var rt = (endTs * 1000 + endMs) - (startTs * 1000 + startMs)
res(rt);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment