进行简单性能测试的更好方法

发布于 2024-07-27 23:54:17 字数 1699 浏览 5 评论 0原文

在比较操作性能时,这就是我通常进行测试的方式:

<?php
$w = 'world';
$start1 = microtime(true);
for($i=0;$i<10000;$i++)
    echo 'Hello ' . $w . '!';
$end1 = microtime(true);

$start2 = microtime(true);
for($i=0;$i<10000;$i++)
    echo "Hello $w!";
$end2 = microtime(true);

$start3 = microtime(true);
for($i=0;$i<10000;$i++)
    echo 'Hello ' + $w + '!';
$end3 = microtime(true);

echo "\n\n\n";
echo 'Concatination: ' . ($end1 - $start1) . "\nInline Var: " . ($end2 - $start2) . "\nAddition Operator: " . ($end3 - $start3);

是否有更好的方法来进行这些测试,可能是更可靠的方法? 似乎如果我多次运行相同的测试,我会得到截然不同的结果,例如我多次运行上述测试,这就是我得到的结果。

Concatination: 0.057300090789795
Inline Var: 0.092978954315186
Addition Operator: 0.090532064437866

Concatination: 0.10458517074585
Inline Var: 0.075299978256226
Addition Operator: 0.039528131484985

Concatination: 0.063031911849976
Inline Var: 0.07781195640564
Addition Operator: 0.022316932678223

Concatination: 0.079019069671631
Inline Var: 0.030484914779663
Addition Operator: 0.096056938171387

Concatination: 0.077842950820923
Inline Var: 0.052779912948608
Addition Operator: 0.037421941757202

Concatination: 0.084203004837036
Inline Var: 0.013757944107056
Addition Operator: 0.074331045150757

Concatination: 0.027930021286011
Inline Var: 0.05648398399353
Addition Operator: 0.049610137939453

Concatination: 0.041821956634521
Inline Var: 0.047034978866577
Addition Operator: 0.062538862228394

Concatination: 0.0071420669555664
Inline Var: 0.066315889358521
Addition Operator: 0.004756927490234

Concatination: 0.088988065719604
Inline Var: 0.022722959518433
Addition Operator: 0.06276798248291

正如您所看到的,每次运行可能会有截然不同的结果。

When comparing the performance of operations this is how I would typicaly do the tests:

<?php
$w = 'world';
$start1 = microtime(true);
for($i=0;$i<10000;$i++)
    echo 'Hello ' . $w . '!';
$end1 = microtime(true);

$start2 = microtime(true);
for($i=0;$i<10000;$i++)
    echo "Hello $w!";
$end2 = microtime(true);

$start3 = microtime(true);
for($i=0;$i<10000;$i++)
    echo 'Hello ' + $w + '!';
$end3 = microtime(true);

echo "\n\n\n";
echo 'Concatination: ' . ($end1 - $start1) . "\nInline Var: " . ($end2 - $start2) . "\nAddition Operator: " . ($end3 - $start3);

Is there a better way to do these tests, possibly a more reliable way? It seems that if I run the same tests several times that I can get wildly different results, for example I ran the above several times and this is what I got.

Concatination: 0.057300090789795
Inline Var: 0.092978954315186
Addition Operator: 0.090532064437866

Concatination: 0.10458517074585
Inline Var: 0.075299978256226
Addition Operator: 0.039528131484985

Concatination: 0.063031911849976
Inline Var: 0.07781195640564
Addition Operator: 0.022316932678223

Concatination: 0.079019069671631
Inline Var: 0.030484914779663
Addition Operator: 0.096056938171387

Concatination: 0.077842950820923
Inline Var: 0.052779912948608
Addition Operator: 0.037421941757202

Concatination: 0.084203004837036
Inline Var: 0.013757944107056
Addition Operator: 0.074331045150757

Concatination: 0.027930021286011
Inline Var: 0.05648398399353
Addition Operator: 0.049610137939453

Concatination: 0.041821956634521
Inline Var: 0.047034978866577
Addition Operator: 0.062538862228394

Concatination: 0.0071420669555664
Inline Var: 0.066315889358521
Addition Operator: 0.004756927490234

Concatination: 0.088988065719604
Inline Var: 0.022722959518433
Addition Operator: 0.06276798248291

As you can see at each running there may be vastly different results.

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

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

发布评论

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

评论(1

诺曦 2024-08-03 23:54:17

尝试 xdebug,它可以在运行时分析您的代码,生成跟踪文件。 然后可以通过 wincachegrind/kcachegrind 或类似的程序运行它,为您提供有关执行方式、花费时间等的详细信息。Zend

Platform 还包括一个分析器(如果您有可用的话)。 我不记得免费的 Zend Debugger 和/或 Zend Server Community Edition 是否也包含分析器,但如果您可以使用它,它是分析代码的另一个选项。

就我个人而言,我更喜欢 xdebug。

Try xdebug, it can profile your code as it runs, producing a trace file. This can then be run through wincachegrind/kcachegrind or similar, to give you details about how your execution peformed, what took the time, etc etc.

Zend Platform also includes a profiler, if you have that available. I can't remember if the free Zend Debugger and/or Zend Server Community Edition includes the profiler as well, but its another option to profile your code if you can get to it.

Personally, I prefer xdebug.

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