当我们改变执行顺序时,performance.now()返回不同的值

时间:2017-04-29 20:28:34

标签: javascript performance performance-testing

我只是试图看到使用||(双管道,短路运算符,应该更快)而不是|的性能差异。

但结果并未对实际的性能改进有所了解。如果我们只是重新排序以下代码...首先使用|执行控制台日志,那么它将显示比另一个更多的时间。

  1. 如何使用此方式或其他方式查看性能差异?此外,

  2. 当我们改变订单时,有人可以说明为什么会有差异?

    如果performance.now();表现得像这样,我在使用它时没有任何意义。

  3. 请帮忙。谢谢:))

    
    
    var t3 = performance.now();
    console.log(1 | (1),
      1 || (0),
      1 || (-1),
      1 || ("a string"),
      1 || (""),
      1 || (null),
      1 || (undefined),
      1 || (console.log(3)),
      1 || (Infinity))
    var t4 = performance.now();
    console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
    
    var t0 = performance.now();
    console.log(1 | (1),
      1 | (0),
      1 | (-1),
      1 | ("a string"),
      1 | (""),
      1 | (null),
      1 | (undefined),
      1 | (console.log(3)),
      1 | (Infinity))
    var t1 = performance.now();
    console.log("Call with single | took " + (t1 - t0) + " milliseconds.");
    
    
    

1 个答案:

答案 0 :(得分:2)

  
      
  1. 如何使用此方式或任何其他方式查看性能差异?
  2.   
  3. 有人可以解释为什么我们改变订单会有区别吗?
  4.   
  1. 做了很多测试。单次测试(尤其是那些花费很少时间的测试)极有可能被您的浏览器或操作系统干扰,这可能会在任何时候决定暂停执行,做其他事情,然后再回来。
  2. 预热环境。几乎所有输入/输出的东西(例如console.log)都需要初始化,这将在第一次使用时完成,这很容易将测量的性能中断几个数量级。
  3. 去掉所有不属于您的测试的东西。如果您想比较|||,请不要在正在计时的区块中包含console.log
  4. 一些示威:

    多次运行以下代码。观察:

    1. 经常(但并非总是)第一个需要更长的时间。这是初始化。
    2. 有时一个人的次数是另一个人的多倍。这是干扰。
    3. 
      
      var t3 = performance.now();
      console.log("herp");
      var t4 = performance.now();
      
      var t0 = performance.now();
      console.log("derp");
      var t1 = performance.now();
      
      console.log("Call with herp took " + (t4 - t3) + " milliseconds.");
      console.log("Call with derp took " + (t1 - t0) + " milliseconds.");
      
      
      

      还要注意I / O对性能有可怕的影响:(你需要向下滚动)

      
      
      var discard = function(){}; // just discards everything
      
      var t3 = performance.now();
      for(var i = 0; i < 1000; ++i)
      {
          discard("");
      }
      var t4 = performance.now();
      
      var t0 = performance.now();
      for(var i = 0; i < 1000; ++i)
      {
          console.log("");
      }
      var t1 = performance.now();
      
      console.log("discard() took " + (t4 - t3) + " milliseconds.");
      console.log("console.log() took " + (t1 - t0) + " milliseconds.");
      &#13;
      &#13;
      &#13;

      考虑到所有这些因素,让我们编写一个测试您的操作的测试用例:

      &#13;
      &#13;
      var discard = function(){};
      
      for(var i = 0; i < 1000; ++i)
      {
          discard(1 | (1),
            1 || (0),
            1 || (-1),
            1 || ("a string"),
            1 || (""),
            1 || (null),
            1 || (undefined),
            1 || (console.log(3)),
            1 || (Infinity));
      }
      for(var i = 0; i < 1000; ++i)
      {
          discard(1 | (1),
            1 | (0),
            1 | (-1),
            1 | ("a string"),
            1 | (""),
            1 | (null),
            1 | (undefined),
            1 | (console.log(3)),
            1 | (Infinity));
      }
      
      var t3 = performance.now();
      for(var i = 0; i < 1000; ++i)
      {
          discard(1 | (1),
            1 || (0),
            1 || (-1),
            1 || ("a string"),
            1 || (""),
            1 || (null),
            1 || (undefined),
            1 || (console.log(3)),
            1 || (Infinity));
      }
      var t4 = performance.now();
      
      var t0 = performance.now();
      for(var i = 0; i < 1000; ++i)
      {
          discard(1 | (1),
            1 | (0),
            1 | (-1),
            1 | ("a string"),
            1 | (""),
            1 | (null),
            1 | (undefined),
            1 | (console.log(3)),
            1 | (Infinity));
      }
      var t1 = performance.now();
      
      console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
      console.log("Call with single | took " + (t1 - t0) + " milliseconds.");
      &#13;
      &#13;
      &#13;

      请注意,完成|案例所需的所有时间仍然来自console.log,证明||上的短路确实有效。当然,这对于实际涉及I / O的| vs ||操作来说是准确的。但是,如果你把它排除在外,结果会再次接近:

      (请注意,我将for循环调整为1000万,证明实际需要多长时间 console.log!)

      &#13;
      &#13;
      var discard = function(){};
      
      for(var i = 0; i < 10000000; ++i)
      {
          discard(1 | (1),
            1 || (0),
            1 || (-1),
            1 || ("a string"),
            1 || (""),
            1 || (null),
            1 || (undefined),
            1 || (Infinity));
      }
      for(var i = 0; i < 10000000; ++i)
      {
          discard(1 | (1),
            1 | (0),
            1 | (-1),
            1 | ("a string"),
            1 | (""),
            1 | (null),
            1 | (undefined),
            1 | (Infinity));
      }
      
      var t3 = performance.now();
      for(var i = 0; i < 10000000; ++i)
      {
          discard(1 | (1),
            1 || (0),
            1 || (-1),
            1 || ("a string"),
            1 || (""),
            1 || (null),
            1 || (undefined),
            1 || (Infinity));
      }
      var t4 = performance.now();
      
      var t0 = performance.now();
      for(var i = 0; i < 10000000; ++i)
      {
          discard(1 | (1),
            1 | (0),
            1 | (-1),
            1 | ("a string"),
            1 | (""),
            1 | (null),
            1 | (undefined),
            1 | (Infinity));
      }
      var t1 = performance.now();
      
      console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
      console.log("Call with single | took " + (t1 - t0) + " milliseconds.");
      &#13;
      &#13;
      &#13;

      尽管如此,|||完全不同。不仅在执行方面,而且在结果方面。请尝试1 || 126 vs 1 | 126"a" || "b" vs "a" | "b"