【问题标题】:PHP string versus boolean speed testPHP字符串与布尔速度测试
【发布时间】:2009-07-15 02:20:01
【问题描述】:

我正在尝试优化 PHP 应用程序中的特定函数,并愚蠢地假设在“if”语句中进行布尔查找会比字符串比较更快。但是为了检查它,我使用microtime 进行了一个简短的测试(见下文)。令我惊讶的是,字符串查找速度更快。

我的测试有什么问题吗(我喝了太多咖啡,所以我怀疑自己的代码)?如果没有,我会对人们在 PHP 中进行字符串与布尔查找有关的任何 cmets 感兴趣。

第一次测试(布尔查找)的结果是 0.168 秒。

第二次测试(字符串查找)的结果是 0.005 秒。

<?php
    $how_many = 1000000;
    $counter1 = 0;
    $counter2 = 0;

    $abc = array('boolean_lookup'=>TRUE, 'string_lookup'=>'something_else');

    $start = microtime();
    for ($i = 0; $i < $how_many; $i++)
    {
        if ($abc['boolean_lookup'])
        {
            $counter1++;
        }
    }

    echo ($start - microtime());

    echo '<hr>';

    $start = microtime();
    for ($i = 0; $i < $how_many; $i++)
    {
        if ($abc['string_lookup'] == 'something_else')
        {
            $counter2++;
        }
    }

    echo ($start - microtime());

【问题讨论】:

  • 你不想使用 microtime() - $start 吗? $start-microtime() 应该是负数。

标签: php string performance boolean


【解决方案1】:

是的,你喝了太多咖啡。您需要使用 microtime(true) 否则您的日期计算以毫秒为单位,但完全忽略秒。另外,使用 current time - start time 来测量持续时间,而不是 start time - current time,否则你得到一个消极的时间。请尝试以下代码:

<?php

$how_many = 5000000;
$counter1 = 0;
$counter2 = 0;

$abc = array('boolean_lookup'=>TRUE, 'string_lookup'=>'something_else');

$start = microtime(true);
for($i = 0; $i < $how_many; $i++)
{
    if($abc['boolean_lookup'])
    {
        $counter1++;
    }

}

echo "FIRST: ", (microtime(true) - $start), "\n";

$start = microtime(true);
for($i = 0; $i < $how_many; $i++)
{
    if($abc['string_lookup'] == 'something_else')
    {
        $counter2++;
    }

}

echo "SECOND: ", (microtime(true) - $start), "\n";

【讨论】:

  • 谢谢!我会关掉咖啡:)
【解决方案2】:

进行一般的if ($var) 不是布尔比较(在某种程度上类似于“字符串比较”)。您可能需要稍微调整一下布尔比较。

使用if ($abc['boolean_lookup'] == TRUE)if ($abc['boolean_lookup'] === TRUE)(2 对 3 等号)再次尝试测试。

我做了一个测试,包括布尔比较,以及三等号比较。 另外一个循环来做 3 次,因为结果可能会因许多外部因素而异。

由于脚本执行的总时间相当长,因此我将$how_many = 5000000; 降低到$how_many = 3000000;

这是最终的脚本:

<?php
function DoTest(){

    $how_many = 3000000;
    $counter1 = 0;
    $counter2 = 0;
    $counter3 = 0;
    $counter4 = 0;
    $counter5 = 0;
    $counter6 = 0;

    $abc = array('boolean_lookup'=>TRUE, 'string_lookup'=>'something_else');

    $start = microtime(true);
    for($i = 0; $i < $how_many; $i++){
        if($abc['boolean_lookup']){
            $counter1++;
        }

    }
    echo "GENERAL-IF ON A BOOL: ", (microtime(true) - $start)*10, "<br />\n";

    $start = microtime(true);
    for($i = 0; $i < $how_many; $i++){
        if($abc['string_lookup']){
            $counter2++;
        }

    }
    echo "GENERAL-IF ON A STRING: ", (microtime(true) - $start)*10, "<br />\n";

    $start = microtime(true);
    for($i = 0; $i < $how_many; $i++){
        if($abc['boolean_lookup'] == TRUE){
            $counter3++;
        }

    }
    echo "TWO-EQUALL-IF ON A BOOL : ", (microtime(true) - $start)*10, "<br />\n";

    $start = microtime(true);
    for($i = 0; $i < $how_many; $i++){
        if($abc['string_lookup'] == 'something_else'){
            $counter4++;
        }

    }
    echo "TWO-EQUALL-IF ON A STRING : ", (microtime(true) - $start)*10, "<br />\n";

    $start = microtime(true);
    for($i = 0; $i < $how_many; $i++){
        if($abc['boolean_lookup'] === TRUE){
            $counter5++;
        }

    }
    echo "THREE-EQUALL-IF ON A BOOL : ", (microtime(true) - $start)*10, "<br />\n";

    $start = microtime(true);
    for($i = 0; $i < $how_many; $i++){
        if($abc['string_lookup'] === 'something_else'){
            $counter6++;
        }

    }
    echo "THREE-EQUALL-IF ON A STRING : ", (microtime(true) - $start)*10, "<br />\n";

}

$number_of_tests = 3;
for($i = 0; $i < $number_of_tests; $i++){
    echo "<br />\n<br />\n== Test #".($i+1)."<br />\n";
    DoTest();
}
?>

结果:

== Test #1
GENERAL-IF ON A BOOL: 7.61245965958
GENERAL-IF ON A STRING: 7.49043941498
TWO-EQUALL-IF ON A BOOL : 8.92991065979
TWO-EQUALL-IF ON A STRING : 10.3996396065
THREE-EQUALL-IF ON A BOOL : 8.02039146423
THREE-EQUALL-IF ON A STRING : 9.25590991974


== Test #2
GENERAL-IF ON A BOOL: 7.74684906006
GENERAL-IF ON A STRING: 7.58201122284
TWO-EQUALL-IF ON A BOOL : 8.90240907669
TWO-EQUALL-IF ON A STRING : 10.2967596054
THREE-EQUALL-IF ON A BOOL : 8.08442115784
THREE-EQUALL-IF ON A STRING : 9.2577290535


== Test #3
GENERAL-IF ON A BOOL: 7.63362884521
GENERAL-IF ON A STRING: 7.5103187561
TWO-EQUALL-IF ON A BOOL : 8.92127037048
TWO-EQUALL-IF ON A STRING : 10.4210495949
THREE-EQUALL-IF ON A BOOL : 8.02319049835
THREE-EQUALL-IF ON A STRING : 9.25379991531

所以,我的结论是,在进行比较时(用等号),毫无疑问,布尔值是赢家。

然而,当做一个简单的普通 if() 语句时,字符串比布尔值更早返回。

有一天,我可能会检查返回 true 或 false 是否存在差异(即检测相反的情况是否需要更长的时间?)

【讨论】:

    猜你喜欢
    • 2013-06-13
    • 2012-01-06
    • 1970-01-01
    • 2014-03-30
    • 2014-06-02
    • 2019-07-15
    • 2012-01-07
    • 2014-02-11
    • 2015-11-15
    相关资源
    最近更新 更多