Timing test for Javascript
HTML
<h1>Javascript timing test</h1>
<p>See the console. Note the difference between the Firefox console and Firebug's console, and a run with no console open.</p>
<p>Note: this page takes about 10 seconds to run.</p>
CSS
table, th, td {
margin: 1em;
padding: 0.2ex 1ex;
}
th {
text-align: right;
}
JavaScript
var numMeasurements = 100;
var measurementList = [];
var startDate = new Date ();
var startTime = startDate.getTime ();
console.log (
startDate.getHours ()+ " : " + startDate.getMinutes () + " : "
+ startDate.getSeconds () + " : " + startDate.getMilliseconds ()
);
var startDate = new Date (); //-- Record time just before interval start.
//setTimeout (timelog, 100);
var logTimer = setInterval (timelog, 100);
function timelog () {
timelog.numloops = timelog.numloops || 0;
if (timelog.numloops >= numMeasurements) {
console.log ('===> Reached ' + timelog.numloops + ' loops.');
clearInterval (logTimer);
//--- Calculate stats:
var stats = {};
stats.min = Number.MAX_VALUE; //-- Always start at opposite
stats.max = Number.MIN_VALUE;
stats.sum = 0;
stats.mean = 0;
stats.sumSqrs = 0;
stats.stdDev = 0;
stats.N = measurementList.length;
for (var J = 0; J < stats.N; ++J) {
var measVal = measurementList[J];
stats.sum += measVal;
stats.sumSqrs += measVal * measVal;
if (measVal > stats.max) stats.max = measVal;
if (measVal < stats.min) stats.min = measVal;
}
stats.mean = stats.sum / stats.N;
stats.stdDev = Math.sqrt (
(stats.sumSqrs / stats.N) - (stats.mean * stats.mean)
);
//--- Display stats:
var decsToDisplay = 2;
console.log (' Measurements: ' + stats.N);
console.log (' Average: ' + stats.mean.toFixed (decsToDisplay) );
console.log (' Min to Max: ' + stats.min + ' to ' + stats.max);
console.log ('Std Deviation: ' + stats.stdDev.toFixed (decsToDisplay) );
$("body").append (
'<table>'
+ ' <tr>'
+ ' ...