jsPerf如何确定最快的代码片段? [英] How does jsPerf determine which of the code snippets is fastest?

查看:40
本文介绍了jsPerf如何确定最快的代码片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. 什么是操作/秒"?
  2. 它执行多少次迭代?
  3. 它以什么为基础计算哪个更快?这些计算背后的公式是什么?

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

有人可以告诉我吗?

解决方案

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

  1. "" ops/sec "代表每秒的操作数.那就是说一个测试计划在一秒钟内执行多少次.

  2. 重复执行测试,直到达到获得小于或等于 1%的百分比不确定度.迭代次数会根据环境计时器的分辨率以及测试在最短运行时间内可以执行多少次而有所不同.我们收集完整的测试运行情况,持续时间 5 (可配置),或者至少 5 运行(也是可配置的),然后对样本进行统计分析.因此,可以在 50 ms (大多数环境的最小运行时间)中重复 100,000 次,然后重复 100乘以( 5 秒).较大的样本量(在此示例中为 100 ),导致较小的错误范围.

  3. 我们还考虑了误差范围,从而决定了哪种测试比ops/sec更快的测试.例如,具有较低操作/秒但具有较高误差范围的测试与具有较高操作/秒且具有较低误差范围的测试在统计上是无法区分的.

    我们使用了 welt t检验,类似于具有统计意义的.

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?

解决方案

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.

这篇关于jsPerf如何确定最快的代码片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆