Created
April 7, 2015 11:05
-
-
Save futjikato/3560b34de20046fd687a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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