There are 14 previous versions of this script.
scr_meta=<><![CDATA[
// ==UserScript==
// @name Measure Function Execution Times
// @namespace http://userscripts.org/users/23652
// @description Measures and compares 2 functions' run-times that you define in the script
// @include http://*.*/*
// @include https://*.*/*
// @include file://*
// @copyright JoeSimmons
// @version 1.0.2
// @license Creative Commons Attribution-Noncommercial 3.0 United States License
// ==/UserScript==
]]></>.toString();
var loops = 10000; // Don't go too high.
// I suggest trying out 10,000 if it's a first function run
// DEFINE FUNCTION 1 HERE
function function1() {
var i=500, arr = new Array(500);
do {
a = 0;
} while(a=arr[--i]);
}
// DEFINE FUNCTION 2 HERE
function function2() {
var arr = new Array(500);
for(var i=0; i<500; i++) {
arr[i] = 0;
}
}
///////// NO EDITING BELOW HERE /////////////////////////////////////
function addCommas(nStr) {
nStr += '';
var x = nStr.split('.'),
x1 = x[0],
x2 = x.length > 1 ? '.' + x[1] : '',
rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function measure1() {
var time=new Date().getTime(), Loops=loops, i=0;
while(++i<Loops) function1();
return (new Date().getTime()) - time;
}
function measure2() {
var time=new Date().getTime(), Loops=loops, i=0;
while(++i<Loops) function2();
return (new Date().getTime()) - time;
}
function esc(e) {
e.target.value = e.target.value.replace(/^.+\n.*\n/,'').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/code\:\n/g,'code:\n<pre>').replace(/\}(?=\n\nFunction 2)/g,'}</pre>').replace(/}$/,'}</pre>');
e.target.removeEventListener('click', esc, false);
}
var before_time = new Date().getTime(),
time_measure_1 = measure1(),
time_measure_2 = measure2(),
diff = (new Date().getTime()) - before_time;
if(time_measure_1 < time_measure_2) {
var result = "Function 1 is " + addCommas((time_measure_2 - time_measure_1)) + " ms ("+(time_measure_2 / time_measure_1).toFixed(2)+" times) faster than Function 2\nUsed " + addCommas(loops) + " loops twice over " + (diff/1000) + " seconds total\nFunction 1 time: "+addCommas(time_measure_1)+" ms\nFunction 2 time: "+addCommas(time_measure_2)+" ms\n\nFunction 1 code:\n"+function1+"\n\nFunction 2 code:\n"+function2;
}
else {
var result = "Function 2 is " + addCommas((time_measure_1 - time_measure_2)) + " ms ("+(time_measure_1 / time_measure_2).toFixed(2)+" times) faster than Function 1\nUsed " + addCommas(loops) + " loops twice over " + (diff/1000) + " seconds total\nFunction 1 time: "+addCommas(time_measure_1)+" ms\nFunction 2 time: "+addCommas(time_measure_2)+" ms\n\nFunction 1 code:\n"+function1+"\n\nFunction 2 code:\n"+function2;
}
var ta = document.createElement('textarea');
ta.addEventListener('dblclick', function(){this.style.display='none';}, false);
ta.addEventListener('click', esc, false);
ta.textContent = '(Single-Click to ready text for posting on Userscripts.org)\n(Double-Click to close these results)\n' + result;
ta.setAttribute('style', 'width:98%; height:50%; padding:4px; font:13px arial; position:fixed;'
+ 'bottom:2px; left:4px;');
document.body.appendChild(ta);
// Auto-Update by sizzlemctwizzle
aaus_38017={
i:'37031', // Script id on Userscripts.org
d:1, // Days to wait between update checks
n:/\/\/\s*@name\s+(.*)\s*\n/i.exec(scr_meta)[1],v:/\/\/\s*@version\s+(.*)\s*\n/i.exec(scr_meta)[1].replace(/\./g, ''),t:new Date().getTime()|0,ca:function(r){GM_xmlhttpRequest({method:'GET',url:'https://userscripts.org/scripts/source/'+this.i+'.meta.js',onload:function(x){aaus_38017.co(x,r)}})},co:function(x,r){this.xv=/\/\/\s*@version\s+(.*)\s*\n/i.exec(x.responseText);this.xn=/\/\/\s*@name\s+(.*)\s*\n/i.exec(x.responseText);if(this.xv&&this.xn[1]==this.n){this.xv=this.xv[1].replace(/\./g, '');this.xn=this.xn[1];}else{if(x.responseText.match('Uh-oh! The page could not be found!')||this.xn[1]!=this.n)GM_setValue('updated', 'off');return false;}if(this.xv>this.v&&confirm('A new version of the '+this.xn+' user script is available. Do you want to update?')){GM_setValue('updated',this.t);GM_openInTab('http://userscripts.org/scripts/source/'+this.i+'.user.js')}else if(this.xv&&this.xv>this.v){if(confirm('Do you want to turn off auto updating for this script?')){GM_setValue('updated','off');GM_registerMenuCommand("Auto Update "+this.n,function(){GM_setValue('updated',new Date().getTime()|0);aaus_38017.ca('return')});alert('Automatic updates can be re-enabled for this script from the User Script Commands submenu.')}else{GM_setValue('updated',this.t)}}else{if(r)alert('No updates available for '+this.n);GM_setValue('updated',this.t)}},ch:function(){if(GM_getValue('updated',0)==0)GM_setValue('updated',this.t);if(GM_getValue('updated',0)!='off'&&+this.t>+GM_getValue('updated',0)+86400000*this.d){this.ca()}else if(GM_getValue('updated',0)=='off'){GM_registerMenuCommand("Enable "+this.n+" updates",function(){GM_setValue('updated',new Date().getTime()|0);aaus_38017.ca(true)})}else{GM_registerMenuCommand("Check "+this.n+" for updates",function(){GM_setValue('updated',new Date().getTime()|0);aaus_38017.ca(true)})}}};if(self.location==top.location&&GM_xmlhttpRequest)aaus_38017.ch();
