I am trying to measure the time taken by various sorting algorithms in JavaScript
I have tried various ways to measure the time taken but it always gives wrong time
var startTime, endTime, timeDiff;
startTime = new Date();
Bubble();
endTime = new Date();
timeDiff = endTime - startTime; //in ms
I have also tried returning the endTime
from Bubble sort function but still the time calculated was wrong
Bubble Sort function
function Bubble()
{
c_delay=0;
for(var i=0;i<array_size-1;i++)
{
for(var j=0;j<array_size-i-1;j++)
{
div_update(divs[j],div_sizes[j],"yellow");//Color update
if(div_sizes[j]>div_sizes[j+1])
{
div_update(divs[j],div_sizes[j], "red");//Color update
div_update(divs[j+1],div_sizes[j+1], "red");//Color update
var temp=div_sizes[j];
div_sizes[j]=div_sizes[j+1];
div_sizes[j+1]=temp;
div_update(divs[j],div_sizes[j], "red");//Height update
div_update(divs[j+1],div_sizes[j+1], "red");//Height update
}
div_update(divs[j],div_sizes[j], "blue");//Color updat
}
div_update(divs[j],div_sizes[j], "green");//Color update
}
div_update(divs[0],div_sizes[0], "green");//Color update
enable_buttons();
return new Date();
}
Calculating time after it has been returned by sort function:
startTime = new Date();
endTime = Bubble();
timeDiff = endTime - startTime; //in ms
I have even tried with performance.now()
but still not getting accurate time taken by the sort function