Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript性能计时。now()_Javascript_Timing - Fatal编程技术网

使用javascript性能计时。now()

使用javascript性能计时。now(),javascript,timing,Javascript,Timing,我试图以毫秒为单位计算函数的执行时间。为此,我使用performance.now()。我可以得到第一次运行的时间,但在第二次、第三次等运行时,我得到0毫秒。 以下是一个例子: function someFunction (){ var t0 = performance.now(); //Function calculations var t1 = performance.now(); Console.log(t1 - t0); } 我启动onclick函数。它在

我试图以毫秒为单位计算函数的执行时间。为此,我使用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()。我只想用毫秒来测量时间

多谢各位

更新 我认为这一切都与速度有关。 例如:

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

函数someFunction(){
var t0=performance.now();
控制台日志(t0);
//函数计算
//一些循环
var计数器=0;
对于(i=0;i<1000000;i++){
计数器++;
}
var t1=performance.now();
控制台日志(t1);
控制台日志(t1-t0);
}
点击我

您使用的实际代码将更改此处的结果,而为什么测试结果为0则是一个猜测问题

这就是说,如今JavaScript中的微基准都受到了限制。例如:

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应用程序中存在真正的性能问题,我会在它运行时对其进行分析,并分析代码中最繁忙的部分。我不是说微基准测试,但是,您在该部分中使用的是,看看是否有适合您的环境的更好的基准测试,最后,在运行该基准测试的同一上下文中,询问其他人有关所讨论的实际代码。

performance.now()已升级,问题应关闭,不再被碰撞

Performance.now()返回的时间戳不限于 一毫秒的分辨率。相反,它们代表的是时间 精度高达微秒的浮点数字


函数someFunction(){
var t0=performance.now();
控制台日志(t0);
//函数计算
//一些循环
var计数器=0;
对于(i=0;i<1000;i++){
计数器++;
}
var t1=performance.now();
控制台日志(t1);
控制台日志(t1-t0);
}
点击我
根据MDN文件:

时间戳实际上不是高分辨率的。为了缓解安全威胁(如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);
函数基准(func){
var start=Date.now()

对于(var i=0;i可能是浏览器在第一次运行时优化了代码,所以下面的代码非常快。我建议您向我们展示您试图在JSFIDLE中度量的实际代码。除了它是
console.log(t1-t0)之外
使用小写的
c
,您显示的代码没有任何错误,因此计时速度太快,以至于显示为
0
ms,或者您没有显示的代码中有其他错误。只需复制您的代码并对其进行测试。每次它都会给我大于0的结果。也许还可以提供详细信息你正在使用的浏览器和操作系统?很有趣。我一直在测试它,大多数时候我都会得到一个正值。但有时我会得到一个负值!我认为这只发生在日期上。如果你没有阅读其他内容,请阅读这里。谢谢你提供的信息。我是Javascript新手,不知道优化器是如何工作的。我想我找出问题所在。再次感谢您为我们指出了正确的方向。