使用javascript performance.now()进行计时

问题描述 投票:3回答:3

我试图以毫秒为单位执行我的函数。我使用performance.now()来做到这一点。我能够在第一次运行时获得时间,但在第二次,第三次,依此类推,我得到0毫秒。这是一个例子:

function someFunction (){
    var t0 = performance.now();
    //Function calculations
    var t1 = performance.now();
    Console.log(t1 - t0);
}

我启动了onclick功能。它在我第一次启动页面时有效。它在第二次点击时停止工作。 t0和t1得到相同的值,当我减去它们时,我得到0的时间。不管怎么说呢?我不一定需要使用performance.now()。我只想以毫秒为单位测量时间。

谢谢。

更新我认为它与速度有关。例如:

    <html>
    <script type="text/javascript">
    	function someFunction (){
        var t0 = performance.now();
        console.log(t0);
        //Function calculations
        //Some loop
        var counter = 0;
        for (i = 0; i < 1000000; i++) { 
        	counter ++;
    	}
        var t1 = performance.now();
        console.log(t1);
        console.log(t1 - t0);
    }
    
    </script>
    
    <button type="button" onclick="someFunction()">Click me</button>
    </hmtl>

按照我的预期工作,但循环for (i = 0; i < 1000; i++)它没有。

谢谢你指出正确的方向。

javascript timing
3个回答
0
投票

您使用的实际代码将更改此处的结果,以及为什么测试结果为0,因为结果是没有它的推测。

也就是说,现在JavaScript中的微基准测试受optimizations的影响。例如:

function spiffy() {
    /* long bit of code that
       loops and loops and runs in 
       O(n!) time then finally */ 
    return result; 
}

漂亮!

让我们说spiffy()确定性地始终输出相同的结果。允许优化器有效地运行它:

function spiffy() { 
    return 42; 
}

转过身来

function someFunction (){
    var t0 = performance.now();
    var result = spiffy();
    var t1 = performance.now();
    Console.log(t1 - t0);
}

进入无用的测试结果。

如果您的JavaScript应用程序中存在真正的性能问题,我会在运行slower than molasses时分析它并分析代码中最繁忙的部分。我并不是指微基准,而是examining run-timelook at the algorithm你在那个部分使用,看看是否有更适合你的情况,最后,在相同的上下文中向其他人询问有关的实际代码它正在运行。


0
投票

performance.now()已升级,问题应该关闭,不再受到影响

https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

Performance.now()返回的时间戳不限于1毫秒的分辨率。相反,它们将时间表示为具有高达微秒精度的浮点数。

  <html>
        <script type="text/javascript">
        	function someFunction (){
            var t0 = performance.now();
            console.log(t0);
            //Function calculations
            //Some loop
            var counter = 0;
            for (i = 0; i < 1000; i++) { 
            	counter ++;
        	}
            var t1 = performance.now();
            console.log(t1);
            console.log(t1 - t0);
        }
        
        </script>
        
        <button type="button" onclick="someFunction()">Click me</button>
        </hmtl>

0
投票

根据MDN doc:

https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

时间戳实际上不是高分辨率。为了缓解诸如Spectre之类的安全威胁,浏览器目前对结果进行了不同程度的舍入。 (Firefox在Firefox 60中开始四舍五入到1毫秒。)有些浏览器也可能会略微随机化时间戳。在未来的版本中,精度可能会再次提高;浏览器开发人员仍在调查这些时间攻击以及如何最好地缓解它们。

在这种情况下,您不应该依赖于浏览器中的performance.now(),或者只能以毫秒分辨率(例如Date.now())来依赖它。

一种解决方法是,使用另一个for{}循环包装代码1000次,因此在包装代码上花费的时间大约是原始代码的1000倍。

function benchmark(func) {
  var start = Date.now()
  for (var i=0;i<1000;i++) {
    func();
  }
  var end = Date.now();
  var diff = (end - start) / 1000;
  console.log('running 1000 times, average time is '+ diff + 'ms');
}
benchmark(someFunction);

或者,如果您的代码没有DOM操作,您可以在NodeJS中测试代码:

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.