簡単なファイル操作を書いていて、文字列のサイズを変数に保存すると速くならないのではないかと考えました。10倍高速であることがわかりました。
このコードの使用:
include "../classes/Timer.class.php";
$t = new Timer(); //Timer class I've written for this purpose [link below]
$multiplyer = 3000000; //Times to try the operation
$string = str_repeat("ggggggggggg",2); //I first tried 2000 here, but for 2 there are same results
$t("calling"); //Saving time
for($i=0; $i<$multiplyer; $i++) {
$size = strlen($string);
$size2 = strlen($string);
$size3 = strlen($string);
}
$t("clover");
$t("caching"); //Saving time
for($i=0; $i<$multiplyer; $i++) {
$size = strlen($string);
$size2 = $size;
$size3 = $size;
}
$t("chover");
$total = $t["calling-clover"]+$t["caching-chover"]; //percents are usefull :)
echo "Calling: {$t["calling-clover"]} (".round(($t["calling-clover"]/$total)*100)."%)<br>\n";
echo "Caching in variables: {$t["caching-chover"]} (".round(($t["caching-chover"]/$total)*100)."%)<br>\n";
結果:
呼び出し: 1.988455057 (67%)
変数へのキャッシュ: 0.984993458 (33%)
さらに興味深いのは、str_repeat
呼び出しに入力した数値は問題ではないため、 はstrlen
明らかに何も計算しないという事実です。サイズはどこかに保存する必要があり、strlen
値を返す関数にすぎません。
これは次のことを意味します:
関数呼び出しは本当に遅いのでしょうか?
そうでない場合、これはstrlen
特定のものですか?
タイマー.class.php