jsPerf如何确定最快的代码片段? [英] How does jsPerf determine which of the code snippets is fastest?
问题描述
今天我访问了 jsPerf ,现在我很想知道……
- 什么是操作/秒"?
- 它执行多少次迭代?
- 它以什么为基础计算哪个更快?这些计算背后的公式是什么?
示例: http://jsperf.com/concatenation-vs-join >
有人可以告诉我吗?
我写了 Benchmark.js ,其中 jsPerf 使用.
-
""
ops/sec
"代表每秒的操作数.那就是说一个测试计划在一秒钟内执行多少次. -
重复执行测试,直到达到获得小于或等于
1%
的百分比不确定度.迭代次数会根据环境计时器的分辨率以及测试在最短运行时间内可以执行多少次而有所不同.我们收集完整的测试运行情况,持续时间5
秒(可配置),或者至少5
运行(也是可配置的),然后对样本进行统计分析.因此,可以在50 ms
(大多数环境的最小运行时间)中重复100,000
次,然后重复100
乘以(5
秒).较大的样本量(在此示例中为100
),导致较小的错误范围. -
我们还考虑了误差范围,从而决定了哪种测试比ops/sec更快的测试.例如,具有较低操作/秒但具有较高误差范围的测试与具有较高操作/秒且具有较低误差范围的测试在统计上是无法区分的.
我们使用了 welt t检验,类似于未配对的2样本t检验,均等方差(方差非常小)因为韦尔奇t检验在将较低的ops/sec和较高的ops/sec与较小的方差进行比较时出现问题,从而导致具有统计意义的.
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?
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.
这篇关于jsPerf如何确定最快的代码片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!