测量和基准测试浏览器中 JavaScript 引擎的处理能力

发布于 2024-11-30 09:50:29 字数 2653 浏览 1 评论 0原文

衡量 v8 或 Spidermonkey 等 JavaScript 引擎性能的准确方法是什么?它至少应该与一个评估和另一个评估没有太大的偏差,可能允许在不同操作系统和不同硬件配置上的不同 JavaScript 引擎之间进行排名。

我的第一次尝试是在一个没有任何内容的网页中进行的,我在网络浏览器中加载了该页面。然后我尝试在 Google Chrome 的 javascript 控制台中执行此代码,结果非常不同,正如您在结果中看到的那样:

mean = function (distr) {
    var sum = 0;
    for (obs in distr) {
        sum += distr[obs];
    };
    return sum / distr.length;
};

stdev = function (distr,mean) {
    var diffsquares = 0;
    for (obs in distr) {
        diffsquares += Math.pow(distr[obs] - mean , 2);
    };
    return Math.sqrt((diffsquares / distr.length));
};


var OPs = 1000000;

var results = [];
for (var t = 0; t < 60; t++) {
    var start = (new Date()).getTime();
    for(var i = 0.5; i < OPs; i++){
        i++;
    }
    var end = (new Date()).getTime();
    var took = end - start;
    var FLOPS = OPs/took;
    results.push(FLOPS);
};

average = mean(results);
deviation = stdev(results,average);

console.log('Average: '+average+' FLOPS. Standart deviation: '+deviation+' FLOPS');

它回复:

NodeJS 0.5.0

  1. 平均:74607.30446024566 FLOPS。标准偏差: 4129.4008527666265 FLOPS
  2. 平均值:73974.89765136827 FLOPS。标准 偏差:4574.367360870471 FLOPS
  3. 平均值:73923.55086434036 FLOPS。 标准偏差:5768.396926072297 FLOPS

Chrome 13.0.782.112(从控制台 (Ctrl+Shift+J))

  1. 平均:1183.409340319158 FLOPS。标准偏差: 24.463468674550658 FLOPS
  2. 平均值:1026.8727431432026 FLOPS。标准 偏差:18.32394087291766 FLOPS
  3. 平均值:1063.7000331534252 失败。标准偏差:22.928786803808094 FLOPS

Chrome 13.0.782.112(作为网页)

  1. 平均:47547.03408688914 FLOPS。标准偏差:4064.7464541422833 FLOPS
  2. 平均值:49273.65762892078 FLOPS。标准偏差:1553.1768207400576 FLOPS
  3. 平均值:47849.72703247966 FLOPS。标准偏差:3445.930694070375 FLOPS

Firefox 6.0

  1. 平均值:62626.63398692811 FLOPS。标准偏差: 3543.4801728588277 FLOPS
  2. 平均值:85572.76057276056 FLOPS。标准 偏差:4336.354514715926 FLOPS
  3. 平均:63780.19323671495 FLOPS。 标准偏差:3323.648677036589 FLOPS

Opera 11.50

  1. 平均值:38462.49044165712 FLOPS。标准偏差: 2438.527900104241 FLOPS
  2. 平均值:37968.736460671964 FLOPS。标准 偏差:2186.9271687271607 FLOPS
  3. 平均:38638.1851173518 FLOPS。 标准偏差:1677.6876987114347 FLOPS

发生了一些奇怪的事情。 Chrome 在控制台上的基准测试比其他浏览器和 NodeJS 中的基准测试花费了更多的时间。我指的是 Chrome 上的 30 秒,而其他浏览器上的 2 秒。与其他浏览器相比,Chrome 在控制台上的标准偏差也非常小。为什么在控制台上执行代码和在网页中执行代码之间存在如此巨大的差异?

如果这太愚蠢了,让我提醒你,我自己“学习”了 javascript(以及一般的编码),而且不久前,所以我在很多事情上都很糟糕。

衡量这一点的好方法是什么?我想关注数学运算的速度,而不是正则表达式速度等其他事情。你有什么推荐?我还尝试生成 10x10 浮点数矩阵并将它们相乘很多次,结果每次都是 7、8 或 9 M FLOPS,但在 Chrome 上大多数是 7,如果它一点也不愚蠢并且有人想要我的代码的话我很乐意粘贴它。

What is an accurate way to measure the performance of a javascript engine like v8 or spidermonkey ? It should at least have not very high deviations from one evaluation and another, probably allow to rank between different javascript engines on different operating systems and different hardware configurations.

My first attempt was this in a web page with nothing on it, I loaded that page in web browsers. Then I tried executing this code in Google Chrome's javascript console and it came out very different as you'll see in the results:

mean = function (distr) {
    var sum = 0;
    for (obs in distr) {
        sum += distr[obs];
    };
    return sum / distr.length;
};

stdev = function (distr,mean) {
    var diffsquares = 0;
    for (obs in distr) {
        diffsquares += Math.pow(distr[obs] - mean , 2);
    };
    return Math.sqrt((diffsquares / distr.length));
};


var OPs = 1000000;

var results = [];
for (var t = 0; t < 60; t++) {
    var start = (new Date()).getTime();
    for(var i = 0.5; i < OPs; i++){
        i++;
    }
    var end = (new Date()).getTime();
    var took = end - start;
    var FLOPS = OPs/took;
    results.push(FLOPS);
};

average = mean(results);
deviation = stdev(results,average);

console.log('Average: '+average+' FLOPS. Standart deviation: '+deviation+' FLOPS');

And it replied:

NodeJS 0.5.0

  1. Average: 74607.30446024566 FLOPS. Standart deviation:
    4129.4008527666265 FLOPS
  2. Average: 73974.89765136827 FLOPS. Standart
    deviation: 4574.367360870471 FLOPS
  3. Average: 73923.55086434036 FLOPS.
    Standart deviation: 5768.396926072297 FLOPS

Chrome 13.0.782.112 (From the Console (Ctrl+Shift+J))

  1. Average: 1183.409340319158 FLOPS. Standart deviation:
    24.463468674550658 FLOPS
  2. Average: 1026.8727431432026 FLOPS. Standart
    deviation: 18.32394087291766 FLOPS
  3. Average: 1063.7000331534252
    FLOPS. Standart deviation: 22.928786803808094 FLOPS

Chrome 13.0.782.112 (as a webpage)

  1. Average: 47547.03408688914 FLOPS. Standart deviation: 4064.7464541422833 FLOPS
  2. Average: 49273.65762892078 FLOPS. Standart deviation: 1553.1768207400576 FLOPS
  3. Average: 47849.72703247966 FLOPS. Standart deviation: 3445.930694070375 FLOPS

Firefox 6.0

  1. Average: 62626.63398692811 FLOPS. Standart deviation:
    3543.4801728588277 FLOPS
  2. Average: 85572.76057276056 FLOPS. Standart
    deviation: 4336.354514715926 FLOPS
  3. Average: 63780.19323671495 FLOPS.
    Standart deviation: 3323.648677036589 FLOPS

Opera 11.50

  1. Average: 38462.49044165712 FLOPS. Standart deviation:
    2438.527900104241 FLOPS
  2. Average: 37968.736460671964 FLOPS. Standart
    deviation: 2186.9271687271607 FLOPS
  3. Average: 38638.1851173518 FLOPS.
    Standart deviation: 1677.6876987114347 FLOPS

Something strange happened. The benchmark in Chrome on the console took a lot more time than the ones in other browsers and NodeJS. I mean something like 30 seconds on Chrome versus 2 on others. The standart deviations in Chrome on the console are also very small compared to others. Why this huge difference between executing the code on the console and executing code in a webpage ?

If this is all too stupid let me remind you that I "learned" javascript (and to code in general) by myself and not very long ago, so I suck at a lot of things.

What is a good measure of this ? I'd like to focus on speed of math operations and not other things like regex speed. What do you recomend ? I also tryied generating 10x10 matrixes of floating point numbers and multiplying them lots of times, the result comes every time either 7, 8 or 9 M FLOPS, but mostly 7 on Chrome, if it's not stupid at all and someone wants the code I'm happy to pastebin it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

云巢 2024-12-07 09:50:29

总体而言,JS 性能优化是一个巨大的领域,从头开始是相当雄心勃勃的。

如果我是你,我会看看这个领域的一些现有项目:

  • Benchmark.js 处理时间和统计分析(平均、计算方差)位。
  • JSPerf 允许任何人创建和运行测试,然后查看任何浏览器的结果。那里有大量的测试存储库可供您仔细阅读。
  • BrowserScope 是 JSPerf 测试的结果存储,并跟踪每个 UA 的结果。

JS performance optimization is a huge area in general, and it's rather ambitious to start from scratch.

If I were you, I'd take a look at some existing projects around this space:

  • Benchmark.js handles the timing and stats analysis (averaging, computing variance) bits.
  • JSPerf lets anyone create and run tests and then look at results for any browser. There's q large repository of tests there that you can peruse.
  • BrowserScope is the results storage for JSPerf tests, and tracks results per-UA.
怀中猫帐中妖 2024-12-07 09:50:29

我认为,Chrome 控制台有一个“奇怪”的执行环境,它并不完全是网页本身,因此会产生一些性能成本。对于 Firefox 中的控制台来说确实如此。

要回答您原来的问题...这实际上取决于您想要测量的内容。不同的 JS 引擎擅长做不同的事情,因此根据测试程序,Chrome 可能比 Firefox 快 5 倍,反之亦然。

此外,浏览器 JIT 所做的优化在很大程度上依赖于整体代码流,因此先执行操作 A 后执行操作 B 所需的时间通常与分别执行 A 和 B 所需的时间总和不同(它可以更大,也可以更小)。因此,对除您实际想要运行的代码之外的任何内容进行基准测试的效用非常有限。运行任何一段代码对于“根据性能对网络浏览器进行排名”几乎毫无用处。

The Chrome console has a "weird" execution environment that's not quite the web page itself and incurs some performance costs due to that, I would think. That's certainly true for the console in Firefox.

To answer your original question... it really depends on what you want to measure. Different JS engines are good at different things, so depending on the test program you could have Chrome being 5x faster than Firefox, say, or vice versa.

Also, the optimizations browser JITs do can be very heavily dependent on the overall code flow, so the time it takes to do operation A followed by operation B is in general not the same as the sum of the times needed to do A and B separately (it can be much larger, or it can be smaller). As a result, benchmarking anything other than the code you actually want to run is of very limited utility. Running any single piece of code is nearly useless for "ranking web browsers according to performance".

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