jsPerf 如何确定哪个代码片段最快?
今天我访问了jsPerf,现在我想知道......
- 什么是“ops/sec”?
- 它进行了多少次迭代?
- 它根据什么计算哪个更快?这些计算背后的公式是什么?
示例: http://jsperf.com/concatenation-vs-join
谁能告诉我吗?
Today I visited jsPerf and now I am wondering…
- What is "ops/sec"?
- How many iterations does it do?
- On what basis does it calculate which is faster? What is the formula behind these calculations?
Example: http://jsperf.com/concatenation-vs-join
Can anyone tell me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我编写了 Benchmark.js,其中 jsPerf 使用。
“
ops/sec
”代表每秒操作数。这是预计一秒钟内执行测试的次数。
重复执行测试,直到达到获得百分比所需的最短时间测量的不确定度小于或等于
1%
。迭代次数将根据环境计时器的分辨率以及测试在最短运行时间内可以执行的次数而变化。我们收集5
秒的已完成测试运行(可配置),或至少5
次运行(也可配置),然后对样本进行统计分析。因此,测试可能会在50 毫秒
内重复100,000
次(大多数环境的最短运行时间),然后重复100
倍(5
秒)。较大的样本量(在本例中为100
)会导致较小的误差范围。我们不仅仅根据每秒的操作数来决定哪个测试更快,还考虑了误差幅度。例如,具有较低操作次数/秒但较高误差范围的测试在统计上可能与具有较高操作次数/秒和较低误差范围的测试无法区分。
我们使用了welch t-test,类似于SunSpider 使用,但切换到 等方差的未配对 2 样本 t 检验(方差极小)因为 welch t 检验在比较较低的操作数/秒和较高的操作数/秒时存在小差异,这导致 自由度计算为小于
1
。我们还在具有相似操作数/秒的测试中添加了5.5%
容差,因为现实世界的测试表明,相同的测试在测试和重新测试之间可能会波动约5%
。 T 检验用于检查检验之间的差异是否具有统计显着性。I wrote Benchmark.js, which jsPerf uses.
"
ops/sec
" stands for operations per second.That is how many times a test is projected to execute in a second.
A test is repeatedly executed until it reaches the minimum time needed to get a percentage uncertainty for the measurement of less than or equal to
1%
. The number of iterations will vary depending on the resolution of the environment’s timer and how many times a test can execute in the minimum run time. We collect completed test runs for5
seconds (configurable), or at least5
runs (also configurable), and then perform statistical analysis on the sample. So, a test may be repeated100,000
times in50 ms
(the minimum run time for most environments), and then repeated100
times more (5
seconds). A larger sample size (in this example,100
), leads to a smaller margin of error.We base the decision of which test is faster on more than just ops/sec by also accounting for margin of error. For example, a test with a lower ops/sec but higher margin of error may be statistically indistinguishable from a test with higher ops/sec and lower margin of error.
We used a welch t-test, similar to what SunSpider uses, but switched to an unpaired 2-sample t-test for equal variance (the variance is extremely small) because the welch t-test had problems comparing lower ops/sec and higher ops/sec with small variances which caused the degrees of freedom to be computed as less than
1
. We also add a5.5%
allowance on tests with similar ops/sec because real world testing showed that identical tests can swing ~5%
from test to re-test. T-tests are used to check that differences between tests are statistically significant.您可以阅读Bulletproof JavaScript 基准测试文章作者。顺便说一句,它使用 Benchmark.js,它是开源的。
You can read Bulletproof JavaScript benchmarks article from the authors. It uses Benchmark.js btw, which is Open Source.