循环 sizeof($x) 与循环 ($x) 的真实速度/基准? [英] The real speed/benchmark of loop sizeof($x) vs loop ($x)?

查看:52
本文介绍了循环 sizeof($x) 与循环 ($x) 的真实速度/基准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 sizeof($x) 与 $x 在循环中的速度感到困惑.这个站点:phpbench.com 声称没有 pre calc -count() 的 sizeof($x) 循环比使用 pre calc count() 慢 数千 个百分点.所以我做了如下测试,但我不确定这是否是测试它的正确方法.结果表明,每个函数的时间几乎相同.所以我不明白它与 phpbench 网站有什么不同.

I'm confused about the speed of the sizeof($x) vs $x when in a loop. This site: phpbench.com claims that the loop of sizeof($x) without pre calc -count() is THOUSANDS of percent slower than with pre calc count(). So I did a test as below, but I'm not sure if this is the right way to test it. The results show that the time is almost the same for each function. So I don't understand how it would be so much different from the phpbench website.

总而言之,如果函数 a(使用 sizeof($unset))确实比函数 b(使用预先计算的 $unset 值)慢得多,我想知道一个明确的答案.我认为现在值/函数存储在内存中,因此 sizeof($x) 实际上可能比几年前的服务器更快?

In summary, I'd like to know a definite answer if function a (with sizeof($unset)) is really considerably slower than function b (with pre calculated $unset value). I think now the values/functions are stored in memory so sizeof($x) could actually be faster than on servers from several years ago?

<?php

$v=0;

function a()
{
//sizeof
$unset=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');

for($i=0;$i<sizeof($unset);$i++) {$v=$v+1;}

return;
}

function b()
{
//pre calculated
$unset=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');

for($i=0;$i<15;$i++) {$v=$v+1;}

return;
}

function benchmark($func)
{

    $start = microtime(true);
    for ($i = 0; $i < 500000; $i++) {
        $func();
    }
    $end = microtime(true);
    $time = $end - $start;

    echo $func . " time: " . sprintf('%.4f', $time) . PHP_EOL.'<br>';
}

benchmark('a'); // sizeof
benchmark('b'); // count

?>

推荐答案

根据我的基准测试 sizeof()count() 表现大致相同,但是要挤您可以执行以下操作以提高循环本身的性能:

According to my benchmarking sizeof() and count() perform about the same, however to squeeze more performance out of the loop itself you can do the following:

for($i=0, $c=sizeof($unset);$i<$c;$i++){
  $v=$v+1;
}

这应该会提升性能,因为它不必在每个循环周期中使用函数进行评估.

This should give it a performance boost because it doesn't have to evaluate with a function in each loop cycle.

我使用的系统是这里,代码如下:>

The system I've used is here with the following code:

$b = new \Benchmark(10000);

$b->register('sizeof', function(){
  $unset=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
  for($i=0;$i<sizeof($unset);$i++) {$v=$v+1;}
});

$b->register('count', function(){
  $unset=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
  for($i=0;$i<count($unset);$i++) {$v=$v+1;}
});

$b->register('count 2', function(){
  $unset=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
  for($i=0, $c=count($unset);$i<$c;$i++) {$v=$v+1;}
});

$b->register('preset', function(){
  $unset=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
  for($i=0;$i<15;$i++) {$v=$v+1;}
});

print_r($b->start());

结果如下:

Array
(
    [stats] => Array
        (
            [phpversion] => 7.1.1
            [itterations] => 40000
            [duration] => 0.1180682182
            [fastest] => preset
            [slowest] => sizeof
        )

    [results] => Array
        (
            [0] => Array
                (
                    [name] => preset
                    [time] => 0.0265829563
                    [average] => 0.0000026583
                    [speed] => 19.73%
                )

            [1] => Array
                (
                    [name] => count 2
                    [time] => 0.0271441936
                    [average] => 0.0000027144
                    [speed] => 18.04%
                )

            [2] => Array
                (
                    [name] => count
                    [time] => 0.0312242508
                    [average] => 0.0000031224
                    [speed] => 5.71%
                )

            [3] => Array
                (
                    [name] => sizeof
                    [time] => 0.0331168175
                    [average] => 0.0000033117
                    [speed] => 0.00%
                )

        )

)

这篇关于循环 sizeof($x) 与循环 ($x) 的真实速度/基准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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