<html><head>
<meta charset="UTF-8">
</head>
<body>
<script>
var Registers = function(){
this.gr = new Uint16Array(8);
this.gr[0]=0;
this.gr[1]=0;
this.gr[2]=0;
this.gr[3]=0;
this.gr[4]=0;
this.gr[5]=0;
this.gr[6]=0;
this.gr[7]=0;
}
var Cpu = function(){
this.regs = new Registers();
this._x = [[0],this.regs.gr,this.regs.gr,this.regs.gr,this.regs.gr,this.regs.gr,this.regs.gr,this.regs.gr];
}
var a;
var cpu = new Cpu();
var i,j;
var startTime = new Date();
for (i = 0; i< 1000000 ;i++){
for (j = 0; j< 8; j++){
a = cpu._x[j][j];
}
}
console.log(new Date() - startTime +"msec");
startTime = new Date();
for (i = 0; i< 1000000 ;i++){
for (j = 0; j< 8; j++){
a = j ===0 ? 0: cpu.regs.gr[j];
}
}
console.log(new Date() - startTime +"msec");
</script>
</body>
</html>
3項演算子のコストが気になって、無理やり配列のアクセスに書き換えてみた結果、ちっとも速くならなかった。
timer.html:33 108msec
timer.html:41 76msec
3項演算子以外と速いみたい。
ちなみにswitch case 文に書き換えると さらに5%くらい早くなるみたい。
timer.html:41 76msec
timer.html:56 72msec
switch が一番はやい。
意外だな。
timer.html:33 108msec
timer.html:41 76msec
3項演算子以外と速いみたい。
ちなみにswitch case 文に書き換えると さらに5%くらい早くなるみたい。
startTime = new Date();
for (i = 0; i< 1000000 ;i++){
for (j = 0; j< 8; j++){
switch(j){
case 0:
a= 0;
break;
default:
a= cpu.regs.gr[j];
}
}
}
console.log(new Date() - startTime +"msec");
timer.html:33 120msectimer.html:41 76msec
timer.html:56 72msec
switch が一番はやい。
意外だな。
0 件のコメント:
コメントを投稿