gpt4 book ai didi

javascript - 定义循环次数 - Benchmark.js

转载 作者:可可西里 更新时间:2023-11-01 01:42:29 36 4
gpt4 key购买 nike

我正在尝试使用 Benchmark.js 执行示例性能基准测试。这是我写的:

var Benchmark = require('benchmark');
var arr = []
benchmark = new Benchmark('testPerf',function(){
arr.push(1000);
},
{
delay: 0,
initCount: 1,
minSamples: 1000,
onComplete : function(){ console.log(this);},
onCycle: function(){}
});
benchmark.run();

现在就像我们在 JUnitBenchmarks 中做的那样:

@BenchmarkOptions(clock = Clock.NANO_TIME, callgc = true, benchmarkRounds = 10, warmupRounds = 1)

我还想在 benchmarkjs 中声明 benchmarkRoundswarmupRounds 计数。我认为 warmupRounds 映射到 initCount?以及如何设置确切的循环/基准迭代次数?

或者如果我们有其他一些可以处理它的优秀 JavaScript 库也可以。

最佳答案

在 JavaScript 基准测试中使用固定的迭代计数是有风险的:we might get zero-time results eventually ,随着浏览器变得更快。

Benchmark.js 不允许提前设置轮数/迭代数。相反,它一遍又一遍地运行测试,直到结果可以被认为是相当准确的。您应该查看 code reading by Monsur Hossain .文章的一些要点:

  • Benchmark.js 中的一个周期包括实际测试的设置、拆卸和多次迭代。
  • Benchmark.js 从分析阶段开始:运行几个周期以找到最佳迭代次数(尽快完成测试,同时收集足够的样本以生成准确的结果)。<
  • 分析期间运行的循环数保存在 Benchmark.prototype.cycles 中.
  • 知道最佳迭代次数后,Benchmark.js 开始采样阶段:运行测试并实际存储结果。
  • Benchmark.prototype.stats.sample是采样期间每个周期的结果数组。
  • Benchmark.prototype.count是采样过程中的迭代次数。

关于javascript - 定义循环次数 - Benchmark.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32629779/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com