1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Nouvelle page 1</title>
</head>
<body>
<script type="text/javascript">
var foo = 1234567890;
function Arn_1(num){
return (num+'').replace(/(\d)(?=(\d{3})+$)/g, '$1 ');
}
function fN_1(num) {
tabnum = (num+'').match(/\d/g)
tnl=tabnum.length+1
tt='';
i=0;
idx=0;
while (--tnl >0){
(tnl%3===0 )&& (tt+=" ");
tt+=tabnum[i++];
}
return tt;
}
function fN_2(num) {
tabnum = (num+'').split('').reverse().join('').match(/\d{1,3}/g).join(' ').split('').reverse().join('')
return tabnum;
}
function fN_3(num) {
num=''+num;
s=(num.length)%3;
return num.substr(0,s)+" "+num.substr(s).match(/\d{3}/g).join(' ');
}
/**
* Figure out how long it takes for a method to execute.
*
* @param {func} method to test
* @param {int} iterations number of executions.
* @param {Array} args to pass in.
* @param {T} context the context to call the method in.
* @return {int} the time it took, in milliseconds to execute.
*/
var bench = function (method, iterations, args, context) {
var time = 0;
var timer = function (action) {
var d = +(new Date);
if (time < 1 || action === 'start') {
time = d;
return 0;
} else if (action === 'stop') {
var t = d - time;
time = 0;
return t;
} else {
return d - time;
}
};
var result = [];
var i = 0;
timer('start');
while (i < iterations) {
result.push(method.apply(context, args));
i++;
}
var execTime = timer('stop');
var output= method.toString().replace(/\n/g, '<br/>')+"<br/>";
output+='<div style="color:green">Temps moyen : ' + execTime / iterations +"<br/>";
output+="Temps Total : "+ execTime +"<br/>";
output+="résultat : "+ result[0]+"<br/><br/></div>";
var res=document.createElement('div')
res.innerHTML=output
document.body.appendChild(res)
return execTime;
};
bench(Arn_1, 10000, [foo], this)
bench(fN_1, 10000, [foo], this)
bench(fN_2, 10000, [foo], this)
bench(fN_3, 10000, [foo], this)
</script>
</body>
</html> |
Partager