六帖のかたすみ

DVを受けていた男性。家を脱出して二周目の人生を生きています。自閉症スペクトラム(受動型)です。http://rokujo.org/ に引っ越しました。

PHP: パフォーマンス改善のためにすべきこと

• Avoid  printf() when  echo is all you need.


• Avoid recomputing values inside a loop, as PHP’s parser does not remove loop invariants. For example, don’t do this if the size of  $array doesn’t change:
for ($i = 0; $i < count($array); $i++) { /* do something */ }


Instead, do this:
$num = count($array);
for ($i = 0; $i < $num; $i++) { /* do something */ }


• Include only files that you need. Split included files to include only functions that you are sure will be used together. Although the code may be a bit more difficult to maintain, parsing code you don’t use is expensive.


• If you are using a database, use persistent database connections—setting up and tearing down database connections can be slow.


• Don’t use a regular expression when a simple string-manipulation function will do the job. For example, to turn one character into another in a string, use str_replace() , not  preg_replace() . 

 

———Programming PHP 3rd edition 325P

 まとめると

・printfよりもechoを使え

・for文の条件に関数を入れるな

・includeするファイルは分割して最小限にしろ

・データベースを開いたらコネクションを使いまわせ

・簡単な文字列操作に正規表現を使うな

となります。ふつうですね。普通すぎます。いつも仕事でやってることと変わりません。パフォーマンスの低下の原因って、どのプログラミング言語でも似たようなもんなんですね。安心しました。