jsPerf 如何确定哪个代码片段最快?

发布于 2024-10-17 08:32:01 字数 326 浏览 2 评论 0原文

今天我访问了jsPerf,现在我想知道......

  1. 什么是“ops/sec”?
  2. 它进行了多少次迭代?
  3. 它根据什么计算哪个更快?这些计算背后的公式是什么?

示例: http://jsperf.com/concatenation-vs-join

谁能告诉我吗?

Today I visited jsPerf and now I am wondering…

  1. What is "ops/sec"?
  2. How many iterations does it do?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⊕婉儿 2024-10-24 08:32:01

我编写了 Benchmark.js,其中 jsPerf 使用。

  1. ops/sec”代表每秒操作数。
    这是预计一秒钟内执行测试的次数。

  2. 重复执行测试,直到达到获得百分比所需的最短时间测量的不确定度小于或等于1%。迭代次数将根据环境计时器的分辨率以及测试在最短运行时间内可以执行的次数而变化。我们收集 5 秒的已完成测试运行(可配置),或至少 5 次运行(也可配置),然后对样本进行统计分析。因此,测试可能会在 50 毫秒 内重复 100,000 次(大多数环境的最短运行时间),然后重复 1005 秒)。较大的样本量(在本例中为100会导致较小的误差范围

  3. 我们不仅仅根据每秒的操作数来决定哪个测试更快,还考虑了误差幅度。例如,具有较低操作次数/秒但较高误差范围的测试在统计上可能与具有较高操作次数/秒和较低误差范围的测试无法区分

    我们使用了welch t-test,类似于SunSpider 使用,但切换到 等方差的未配对 2 样本 t 检验(方差极小)因为 welch t 检验在比较较低的操作数/秒和较高的操作数/秒时存在小差异,这导致 自由度计算为小于1。我们还在具有相似操作数/秒的测试中添加了 5.5% 容差,因为现实世界的测试表明,相同的测试在测试和重新测试之间可能会波动约5%。 T 检验用于检查检验之间的差异是否具有统计显着性

I wrote Benchmark.js, which jsPerf uses.

  1. "ops/sec" stands for operations per second.
    That is how many times a test is projected to execute in a second.

  2. 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 for 5 seconds (configurable), or at least 5 runs (also configurable), and then perform statistical analysis on the sample. So, a test may be repeated 100,000 times in 50 ms (the minimum run time for most environments), and then repeated 100 times more (5 seconds). A larger sample size (in this example, 100), leads to a smaller margin of error.

  3. 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 a 5.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.

丑丑阿 2024-10-24 08:32:01

您可以阅读Bulletproof JavaScript 基准测试文章作者。顺便说一句,它使用 Benchmark.js,它是开源的。

You can read Bulletproof JavaScript benchmarks article from the authors. It uses Benchmark.js btw, which is Open Source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文