Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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中测试函数计算速度最准确的方法是什么?_Javascript_Performance_Performance Testing - Fatal编程技术网

在JavaScript中测试函数计算速度最准确的方法是什么?

在JavaScript中测试函数计算速度最准确的方法是什么?,javascript,performance,performance-testing,Javascript,Performance,Performance Testing,我需要比较一些代码的计算速度,但我不确定最好的方法是什么。是否有某种内置计时器来执行此操作?有没有可能以纳秒为单位获得计算速度,或者我需要处理JavaScript通常使用的毫秒数?您可以使用Internet explorer的开发者工具脚本分析器。我相信Chrome和Firefox也有类似的工具。它会在几毫秒内给你答案。但是如果所花费的时间少于一毫秒,那么我认为它也会以纳秒为单位显示。您可以使用Internet explorer的开发者工具脚本分析器。我相信Chrome和Firefox也有类似的

我需要比较一些代码的计算速度,但我不确定最好的方法是什么。是否有某种内置计时器来执行此操作?有没有可能以纳秒为单位获得计算速度,或者我需要处理JavaScript通常使用的毫秒数?

您可以使用Internet explorer的开发者工具脚本分析器。我相信Chrome和Firefox也有类似的工具。它会在几毫秒内给你答案。但是如果所花费的时间少于一毫秒,那么我认为它也会以纳秒为单位显示。

您可以使用Internet explorer的开发者工具脚本分析器。我相信Chrome和Firefox也有类似的工具。它会在几毫秒内给你答案。但是,如果所用的时间少于一毫秒,那么我认为它也会以纳秒为单位显示。

您可能正在寻找
控制台。time
。请参阅。

您可能正在查找
控制台。时间
。请参阅。

您可以尝试浏览器内置的评测功能:


它们具有丰富的功能,可以开箱即用,无需编写一行代码

您可以尝试浏览器内置的评测功能:

它们功能丰富,可以开箱即用,无需编写一行代码。您所寻找的可能是
性能。现在()
,这将为您提供微秒的感知

Performance.now()
方法返回一个 以毫秒为单位,精确到千分之一毫秒,等于 事件发生后的毫秒数 属性和对方法的调用

MDN提供的示例是:

var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
一个可用于多次测试特定短代码段性能的函数,可以使用以下各项:

/**
 * Finds the performance for a given function
 * function fn the function to be executed
 * int n the amount of times to repeat
 * return array [time elapsed for n iterations, average execution frequency (executions per second)]
 */
function getPerf(fn, n) {
  var t0, t1;
  t0 = performance.now();
  for (var i = 0; i < n; i++) {
    fn(i)
  }
  t1 = performance.now();
  return [t1 - t0, repeat * 1000 / (t1 - t0)];
}
/**
*查找给定函数的性能
*函数fn要执行的函数
*int n重复的次数
*返回数组[n次迭代所用的时间,平均执行频率(每秒执行次数)]
*/
函数getPerf(fn,n){
var-t0,t1;
t0=性能。现在();
对于(变量i=0;i
它返回单个执行
fn(i)
所需的时间(毫秒)和执行频率(每秒执行次数)。
n
值越高,精度越高,但测试时间越长。参数
i
可以包含在要测试的函数中,该函数包含函数的当前迭代。

我偶然发现了这条路。您所寻找的可能是
性能。现在()
,这将为您提供微秒的感知

Performance.now()
方法返回一个 以毫秒为单位,精确到千分之一毫秒,等于 自 属性和对方法的调用

MDN提供的示例是:

var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
一个可用于多次测试特定短代码段性能的函数,可以使用以下各项:

/**
 * Finds the performance for a given function
 * function fn the function to be executed
 * int n the amount of times to repeat
 * return array [time elapsed for n iterations, average execution frequency (executions per second)]
 */
function getPerf(fn, n) {
  var t0, t1;
  t0 = performance.now();
  for (var i = 0; i < n; i++) {
    fn(i)
  }
  t1 = performance.now();
  return [t1 - t0, repeat * 1000 / (t1 - t0)];
}
/**
*查找给定函数的性能
*函数fn要执行的函数
*int n重复的次数
*返回数组[n次迭代所用的时间,平均执行频率(每秒执行次数)]
*/
函数getPerf(fn,n){
var-t0,t1;
t0=性能。现在();
对于(变量i=0;i
它返回单个执行
fn(i)
所需的时间(毫秒)和执行频率(每秒执行次数)。
n
值越高,精度越高,但测试时间越长。参数
i
可以包含在要测试的函数中,该函数包含函数的当前迭代。

您应该使用来获得具有统计意义的结果。由于浏览器中涉及各种JIT系统,性能结果可能会有很大差异,因此jsPerf或它使用的Benchmarking.js库将根据需要多次运行代码以获得良好的结果。但是请注意,如果一个函数在几纳秒内运行,那么担心它的性能几乎总是没有必要的。

您应该使用它来获得统计上显著的结果。由于浏览器中涉及各种JIT系统,性能结果可能会有很大差异,因此jsPerf或它使用的Benchmarking.js库将根据需要多次运行代码以获得良好的结果。但是请注意,如果一个函数在几纳秒内运行,那么担心它的性能几乎总是没有必要的。

一个额外的工具是重新使用Mocha单元测试。

一个额外的工具是重新使用Mocha单元测试。

这仍然是以毫秒为单位的,但这看起来像是一个内置调试函数,所以我想我必须处理毫秒数,然后再运行循环一点迭代。这仍然是毫秒数,但这看起来像是一个内置调试函数,所以我想我必须处理毫秒数,然后运行循环进行更多的迭代。