gpt4 book ai didi

javascript - 如何显示/读取结果(ops/sec)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:25 25 4
gpt4 key购买 nike

我能够成功创建和运行基准套件,但不确定如何获取每个输出的基准值,this.filter('fastest').pluck('name')onComplete 给出了最快操作的名称,但我想要测试套件中每个函数的 ops/sec 值。如何获得?

最佳答案

在您的 onComplete 回调中,您可以通过 this 关键字(它将引用当前的 BenchmarkSuite 对象)访问您的基准测试,如下所示:

var bench1 = this[0];
var bench2 = this[1];
...
var benchN = this[N-1];

每个 bench 都是 Benchmark 的实例.所以你可以获得任何你想要的信息(参见 Benchmark.prototype )。要获取 ops/sec 值,请使用 .hz基准的属性。一个更好理解的小例子:

new Benchmark.Suite()
.add('test1', function() {
// some code
})
.add('test2', function() {
// some code
})
.on('complete', function() {
var benchTest1 = this[0]; // gets benchmark for test1
var benchTest2 = this[1]; // gets benchmark for test2

console.log(benchTest1.hz); // ops/sec
// benchmark info in format:
// test2 x 1,706,681 ops/sec ±1.18% (92 runs sampled)
console.log(benchTest2.toString());

console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run();

关于javascript - 如何显示/读取结果(ops/sec)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26792362/

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