配列に格納されたデータを使用して HTML を作成するクラスがあります。この配列には約 100 個の項目があります。各項目には、会社名、説明、会社がサポートするさまざまなプログラミング言語のフラグなどの情報が含まれています。各アイテムの HTML を作成するときに、文字列の連結を行っています。
プログラミング言語データを追加すると、パフォーマンスが突然大幅に低下することに気付きました。ページ レンダリング タイマーが 0.15 秒から ~0.60 秒に急上昇しています。この時間には、データベースから毎回同じデータを取得する時間が含まれます。これらの 2 回の間で一貫してパフォーマンスをジャンプさせることができますが、次のコード行をコメント/コメント解除します。
$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;
}
}