PHP与字符串相关的性能问题

时间:2012-07-05 14:36:13

标签: php string performance

我有一个类使用存储在数组中的数据构建一些HTML。此阵列中有大约100个项目。每个项目都包含公司名称,描述和公司支持的不同编程语言的标志等信息。当我为每个项目构建HTML时,我正在进行字符串连接。

我注意到,当我附加编程语言数据时,性能突然受到重创。我看到页面渲染计时器从0.15秒跳到~0.60秒。这一次包括每次从数据库中获取相同的数据。我可以始终如一地将性能提升到这两次之间但是注释/取消注释以下代码行:

$html .= '<div class="programmingLanguages"><strong>Programming Languages</strong> '.implode(', ', $progLanguagesArray).'</div>';

通过添加一个长测试字符串,我也能够获得相同的性能下降,如下所示:

$html .= 'testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest';

特别奇怪的是,我有另一行使用相同“内爆”功能的代码并且不会对性能产生任何显着影响:

$html .= '<div class="integrationMethods"><strong>Integration Methods:</strong> '.implode(', ', $intMethodsArray).'</div>';

有没有人对这里可能发生的事情有所了解?我在我的代码中的其他地方做了大量的连接,之前没有见过这样的东西。在这一点上,我很难过。

这是完整的课程:

class DeveloperView {

public static function getHtml($developers) {
    $html = '';
    $html .= '<div>';
    $html .= '<div>';

    $count = 0;
    foreach ($developers as $developer) {
        $url = $developer['attributes']['url'];
        $phone = $developer['attributes']['phone'];
        $company = $developer['attributes']['desc'];
        $active = $developer['attributes']['active'];
        $desc = $developer['object_value'];

        $intMethodsArray = array();
        if ($developer['attributes']['m1']) { $intMethodsArray[] = 'method 1'; }
        if ($developer['attributes']['m2']) { $intMethodsArray[] = 'method 2'; }
        if ($developer['attributes']['m3']) { $intMethodsArray[] = 'method 3'; }
        if ($developer['attributes']['m4']) { $intMethodsArray[] = 'method 4'; }
        if ($developer['attributes']['m5'])     { $intMethodsArray[] = 'method 5'; }

        $progLanguagesArray = array();
        if ($developer['attributes']['dotnet']) { $progLanguagesArray[] = '.Net (C# or VB.Net)'; }
        if ($developer['attributes']['asp'])    { $progLanguagesArray[] = 'Classic ASP'; }
        if ($developer['attributes']['cf'])     { $progLanguagesArray[] = 'Cold Fusion'; }
        if ($developer['attributes']['java'])   { $progLanguagesArray[] = 'Java'; }
        if ($developer['attributes']['php'])    { $progLanguagesArray[] = 'PHP'; }
        if ($developer['attributes']['perl'])   { $progLanguagesArray[] = 'Perl'; }
        if ($developer['attributes']['other'])  { $progLanguagesArray[] = 'Other'; }

        $html .= '<div class="';
        if ($count % 2 == 0) {
            $html .= 'listingalt';
        } else {
            $html .= 'listing';
        }
        $html .= '">';

        $html .= '<div class="developerPhone">'.$phone.'</div>';
        $html .= '<a class="ext_link" target="_blank" href="'.$url.'">'.$company.'</a>';

        $html .= '<div>';
        if (!empty($intMethodsArray)) {
            $html .= '<div class="integrationMethods"><strong>Integration Methods:</strong> '.implode(', ', $intMethodsArray).'</div>';
        }

        if (!empty($progLanguagesArray)) {
            $html .= '<div class="programmingLanguages"><strong>Programming Languages</strong> '.implode(', ', $progLanguagesArray).'</div>';
        }

        $html .= '</div>';

        $html .= '<p>'.$desc.'</p>';

        $html .= '</div>'."\n";

        $count++;
    }

    $html .= '</div></div>';

    return $html;
}

}

1 个答案:

答案 0 :(得分:0)

既然我可以提供答案,我会将我的后续评论作为'答案'发布......

我确实在我的计时器中有一个'错误',因为它计算了HTML回声之后的结束处理时间。因此,发送到浏览器的数据量正在影响处理时间,我希望在传输任何数据之前看到处理时间。