I am trying to time the execution of my function in milliseconds. I use performance.now() in order to do that. I am able to get the time on the first run, but on the second, third, and so on runs I get 0 milliseconds. Here is an example:
function someFunction (){
var t0 = performance.now();
//Function calculations
var t1 = performance.now();
Console.log(t1 - t0);
}
I launch the function onclick. It works when I first launch the page. It stops working on the second click. t0 and t1 get the same values and when I subtract them I get 0 for the time. Is there anyway around it? I don't necessarily need to use performance.now(). I just want to measure time in milliseconds.
Thank you.
Update I think it has everything to do with the speed. For example:
<html>
<script type="text/javascript">
function someFunction (){
var t0 = performance.now();
console.log(t0);
//Function calculations
//Some loop
var counter = 0;
for (i = 0; i < 1000000; i++) {
counter ++;
}
var t1 = performance.now();
console.log(t1);
console.log(t1 - t0);
}
</script>
<button type="button" onclick="someFunction()">Click me</button>
</hmtl>
Works as I would expect, but with the loop for (i = 0; i < 1000; i++)
it doesn't.
Thank you for the pointers in the right direction.
console.log(t1 - t0)
with a lowercasec
, there is nothing wrong with the code you have shown so the timing is either literally so fast that it comes out as0
ms or there is something else wrong in the code you are not showing. – Splashy