search

Home  >  Q&A  >  body text

javascript - for循环加速

受@wangfulin和@SyuTingSong的启发,改了下代码,
经测试,sumAll2更耗时,而在node环境下并无太大差异

    function sumAll(arr) {
        var i, sum = 0;
        for (i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }

    function sumAll2(arr) {
        var len = arr.length,
            sum = 0;
        for (; len> 0;) {
            sum += arr[--len];
        }
        return sum;
    }

    var num = [],
        t2sum = 0,
        t4sum = 0,
        n = 10; //测试次数

    for (i = 0; i < 10000000; i++) {
        num[i] = parseInt(Math.random() * 100000);
    }


    function test() {
        var t1 = (new Date()).getTime();
        var x = sumAll(num);
        var t2 = (new Date()).getTime() - t1;
        t2sum += t2;

        var t3 = (new Date()).getTime();
        var y = sumAll2(num);
        var t4 = (new Date()).getTime() - t3;
        t4sum += t4;

        console.log('x:' + x);
        console.log('y:' + y);
        console.log('t2:' + t2);
        console.log('t4:' + t4);
    }

    for (i = 0; i < n; i++) {
        test();
    }
    console.log('###############################');
    console.log('t2avg:' + t2sum / n);
    console.log('t4avg:' + t4sum / n);

为神马sumAll2更耗时间

PHP中文网PHP中文网2817 days ago189

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-10 16:40:44

    我修改一下答案....看来顺序读和倒序读并没有区别.
    我在nodejs下试了试, 两个时间消耗是一样的.

    function sumAll3(arr) {
      var left = arr.length % 8, sum = 0;
      var i = 0, length = arr.length;
      for (; i < length;) {
        sum += arr[i + 0];
        sum += arr[i + 1];
        sum += arr[i + 2];
        sum += arr[i + 3];
        sum += arr[i + 4];
        sum += arr[i + 5];
        sum += arr[i + 6];
        sum += arr[i + 7];
    
        i += 8;
      }
      switch (left) {
        case 7: sum += arr[i++];
        case 6: sum += arr[i++];
        case 5: sum += arr[i++];
        case 4: sum += arr[i++];
        case 3: sum += arr[i++];
        case 2: sum += arr[i++];
        case 1: sum += arr[i++];
      }
      return sum;
    }

    我写了一个手动内联的版本, 表示在nodejs下面, 性能和第一个第二个没有显著差别......nodejs优化还是挺好的

    reply
    0
  • 高洛峰

    高洛峰2017-04-10 16:40:44

    多测几次你会发现,t2有可能比t4小,也可能比t4大,len在循环内计算和循环外直接计算好对性能影响很小,所以受计算环境的影响可能会影响最终的结果,而这种结果也是不可预期的,所以没法作结论说sumAll2更耗时间或者sunAll2更不耗时间

    reply
    0
  • PHP中文网

    PHP中文网2017-04-10 16:40:44

    改了一下你的程序,方便多次运行

    function sumAll(arr) {
        var i, sum = 0;
        for (i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }
    
    function sumAll2(arr) {
        var len = arr.length,
            sum = 0;
        for (; len > 0;) {
            sum += arr[--len];
        }
        return sum;
    }
    
    function test() {
        var num = [];
        for (var i = 0; i < 10000000; i++) {
            num[i] = parseInt(Math.random() * 100000);
        }
        var t1 = (new Date()).getTime();
        var x = sumAll(num);
        var t2 = (new Date()).getTime() - t1;
    
        var t3 = (new Date()).getTime();
        var y = sumAll2(num);
        var t4 = (new Date()).getTime() - t3;
    
        console.log('x:' + x);
        console.log('y:' + y);
        console.log('t2:' + t2);
        console.log('t4:' + t4);
    }
    
    for (var i = 0; i < 4; i++) {
        test();
    }

    下面是我电脑上的运行结果

    x:499999293271
    y:499999293271
    t2:21
    t4:20
    x:499911919107
    y:499911919107
    t2:25
    t4:19
    x:500178188376
    y:500178188376
    t2:20
    t4:20
    x:499878909169
    y:499878909169
    t2:20
    t4:20

    说明运行耗时有波动

    reply
    0
  • Cancelreply