Created
February 16, 2010 12:00
-
-
Save bga/305479 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
function repeatStringBinary(str, how_many) | |
{ | |
how_many|=0; // convert to number and round | |
str+=""; // convert to string | |
if(how_many<=0 || str.length==0) | |
return ""; | |
if(how_many==1) | |
return str; | |
if(how_many==2) | |
return str+str; | |
var result=""; | |
while(how_many) | |
{ | |
if(how_many&1) | |
result+=str; | |
how_many>>=1; | |
str+=str; | |
} | |
return result; | |
}; | |
function repeatStringLinear(str, how_many) { | |
var result = ''; | |
for (var i = 0; i < how_many; i++) | |
result += str; | |
return result; | |
} | |
var i,n=100000; | |
var a="a",m=100; | |
function _clear(a,m,n) | |
{ | |
var i,d0=+new Date(); | |
for(i=0;i<n;++i) | |
; | |
return new Date()-d0; | |
}; | |
function _linear(a,m,n) | |
{ | |
var i,d0=+new Date(); | |
for(i=0;i<n;++i) | |
repeatStringLinear(a,m); | |
return new Date()-d0; | |
}; | |
function _binary(a,m,n) | |
{ | |
var i,d0=+new Date(); | |
for(i=0;i<n;++i) | |
repeatStringBinary(a,m); | |
return new Date()-d0; | |
}; | |
if(window.console==null) | |
window.console={}; | |
if(console.log==null) | |
console.log=function(msg){ alert(msg); }; | |
function _pointTest(a,m,n) | |
{ | |
var clear=_clear(a,m,n); | |
console.log("linear "+(_linear(a,m,n)-clear)); | |
console.log("binary "+(_binary(a,m,n)-clear)); | |
}; | |
function _linearTest(a,n,x,y,step) | |
{ | |
var clears=[]; | |
for(var m=x;m<y;m+=step) | |
clears.push(_clear(a,m,n)); | |
var t=""; | |
for(var m=x;m<y;m+=step) | |
t+=m+"\t"; | |
console.log("m\t\t "+t); | |
t=""; | |
for(var m=x,i=0;m<y;m+=step,++i) | |
t+=(_linear(a,m,n)-clears[i])+"\t"; | |
console.log("linear\t\t "+t); | |
t=""; | |
for(var m=x,i=0;m<y;m+=step,++i) | |
t+=(_binary(a,m,n)-clears[i])+"\t"; | |
console.log("binary\t\t "+t); | |
}; | |
function _binaryTest(a,n,x,y,step) | |
{ | |
var clears=[]; | |
for(var m=x;m<y;m*=step) | |
clears.push(_clear(a,m,n)); | |
var t=""; | |
for(var m=x;m<y;m*=step) | |
t+=m+"\t"; | |
console.log("m\t\t "+t); | |
t=""; | |
for(var m=x,i=0;m<y;m*=step,++i) | |
t+=(_linear(a,m,n)-clears[i])+"\t"; | |
console.log("linear\t\t\ "+t); | |
t=""; | |
for(var m=x,i=0;m<y;m*=step,++i) | |
t+=(_binary(a,m,n)-clears[i])+"\t"; | |
console.log("binary\t\t "+t); | |
}; | |
//_pointTest("a",3,100000); | |
/* | |
ff3.6 | |
linear 723 | |
binary 861 | |
chrome 4.0.249.89 (38071) | |
linear 188 | |
binary 165 | |
ie7 | |
linear 1482 | |
binary 1933 | |
opera 10.10 | |
linear 501 | |
binary 1111 | |
opera 10.50 | |
linear 208 | |
binary 218 | |
*/ | |
//_linearTest("a",20000,1,16,2); | |
/* | |
ff3.6 | |
m 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
linear 115 114 122 142 159 175 179 208 216 222 246 250 266 271 296 | |
binary 85 111 216 248 245 231 229 216 236 226 263 233 257 250 273 | |
chrome 4.0.249.89 (38071) | |
m 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
linear 16 16 22 31 37 39 44 48 53 62 67 69 80 84 91 | |
binary 13 19 34 33 39 32 48 41 41 46 48 42 51 47 52 | |
ie7 _linearTest("a",5000,1,16,2); | |
m 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
linear 80 100 90 80 100 120 131 130 150 180 181 190 190 210 231 | |
binary 50 60 100 90 110 111 120 110 130 130 140 121 130 140 140 | |
opera 10.10 | |
m 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
linear 60 40 231 70 250 90 291 120 300 301 330 161 350 341 360 | |
binary 231 50 110 300 301 130 320 301 320 311 350 321 340 331 340 | |
opera 10.50 | |
m 1 3 5 7 9 11 13 15 | |
linear 8 32 45 57 85 90 101 115 | |
binary 7 41 53 66 53 78 76 92 | |
*/ | |
//_binaryTest("a",20000,1,129,2); | |
/* | |
ff3.6 | |
m 1 2 4 8 16 32 64 128 | |
linear 144 104 143 199 305 511 910 1778 | |
binary 85 104 182 217 244 275 566 374 | |
chrome 4.0.249.89 (38071) | |
m 1 2 4 8 16 32 64 128 | |
linear 15 16 29 49 100 180 377 736 | |
binary 13 20 31 40 46 50 56 74 | |
ie7 _binaryTest("a",5000,1,129,4); | |
m 1 4 16 64 | |
linear 80 130 260 882 | |
binary 50 100 130 150 | |
opera 10.10 | |
m 1 2 4 8 16 32 64 128 | |
linear 50 40 81 270 381 701 1362 2493 | |
binary 70 30 291 300 341 340 551 911 | |
opera 10.50 | |
m 1 2 4 8 16 32 64 128 | |
linear 7 18 29 63 121 226 423 815 | |
binary 7 18 41 56 67 73 100 154 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment