0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

#プログラミング #Javascript #nodejs #chrome ビット演算・算術演算、領域管理の速度差

Posted at

本内容を利用した場合の一切の責任を私は負いません。

いつもコーディングする時に、コンバイル言語の名残で、書き方で違いがでるか迷うので実験してみた。
実験結果から、ソースが違うだろうから、node.jsとChromeではやはり挙動に違いがありそう。
書き方は意識した方がいいのかも。
最適化もありそうな気がする。

バージョン

  • OS
    OS 名: Microsoft Windows 10 Home
    OS バージョン: 10.0.19045 N/A ビルド 19045
    システムの種類: x64-based PC
  • node.js
    node-v20.9.0-win-x64.zip
  • Chrome
    バージョン: 125.0.6422.114(Official Build) (64 ビット)

#ソース

a.js
function aa() {
    let total;

    console.time("elap3");
    total = 0;

    for (let indexA = 0; indexA < 10; indexA++) {
        for (let index = 0; index < 1000 * 1000 * 1000; index++) {
            let c = index & 0xff;
            total += c & 1;
        }
    }

    console.timeEnd("elap3")
    console.log(total);

    console.time("elap4");
    total = 0;

    for (let indexA = 0; indexA < 10; indexA++) {
        for (let index = 0; index < 1000 * 1000 * 1000; index++) {
            let d = index % 0x100;
            total += d & 1;
        }
    }

    console.timeEnd("elap4")
    console.log(total);

    console.time("elap5");
    total = 0;

    for (let indexA = 0; indexA < 10; indexA++) {
        for (let index = 0; index < 1000 * 1000 * 1000; index++) {
            let d = index % 0xff;
            total += d & 1;
        }
    }

    console.timeEnd("elap5")
    console.log(total);

    console.time("elap1");
    total = 0;
    let a;

    for (let indexA = 0; indexA < 10; indexA++) {
        for (let index = 0; index < 1000 * 1000 * 1000; index++) {
            a = index;
            total += a & 1;
        }
    }

    console.timeEnd("elap1")
    console.log(total);

    console.time("elap2");
    total = 0;

    for (let indexA = 0; indexA < 10; indexA++) {
        for (let index = 0; index < 1000 * 1000 * 1000; index++) {
            let b = index;
            total += b & 1;
        }
    }

    console.timeEnd("elap2")
    console.log(total);
    return;
}

//ブラウザ時はコメントアウト
aa();

出力

Chrome
a.js:14 elap3: 11638.322998046875 ms
a.js:15 5000000000
a.js:27 elap4: 12354.191162109375 ms
a.js:28 5000000000
a.js:40 elap5: 17977.928955078125 ms
a.js:41 4980392160
a.js:54 elap1: 12879.364013671875 ms
a.js:55 5000000000
a.js:67 elap2: 13187.98095703125 ms
a.js:68 5000000000
node.js
elap3: 11549.76806640625 ms
elap3: 11.550s
5000000000
elap4: 11177.266845703125 ms
elap4: 11.177s
5000000000
elap5: 14984.0810546875 ms
elap5: 14.984s
4980392160
elap1: 11331.4638671875 ms
elap1: 11.332s
5000000000
elap2: 11503.802978515625 ms
elap2: 11.505s
5000000000
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?