ホームページ >バックエンド開発 >PHPチュートリアル >PHP ファイル キャッシュには 3 つの形式が含まれています_PHP チュートリアル
PHP ファイルのキャッシュ内容を保存するには、次の 3 つの主な形式があります:
1. 変数 var_export は、使用する場合、ファイルを直接インクルードします
2。シリアル化後に使用します
3 を使用すると、変数 json_encode がフォーマットされて保存されます。 json_decode を使用する場合、私は常に最初の方法が最も効率的であると考えていました。ネイティブである必要があり、少なくともキャッシュの読み取り効率は最も高いはずですが、今日テストして衝撃を受けました。読み取りでも書き込みでも、serialize が最もシリアル化効率が高いことがわかりました。
PHP ファイル キャッシュのテストに使用したコードは次のとおりです:
<ol class="dp-xml"> <li class="alt"><span><span>view plaincopy to clipboardprint? </span></span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li><span>/* </span></li> <li class="alt"> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">var_export</span><span>($_SERVER,1); </span> </li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">""</span><span>; </span> </li> <li class="alt"><span>file_put_contents("data/in.php",$file); </span></li> <li><span>*/ </span></li> <li class="alt"><span>include("data/in.php"); </span></li> <li><span>} </span></li> <li class="alt"><span>echo "include读:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_put_contents</span><span>("data/se.php" </span> </li> <li class="alt"><span>,serialize($_SERVER)); </span></li> <li> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_get_contents</span><span>("data/se.php"); </span> </li> <li class="alt"> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">unserialize</span><span>($file); </span> </li> <li><span>} </span></li> <li class="alt"><span>echo "serialize写:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_put_contents</span><span>("data/se. </span> </li> <li class="alt"><span>php",serialize($_SERVER)); </span></li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_get_contents</span><span>("data/se.php"); </span> </li> <li class="alt"> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">unserialize</span><span>($file); </span> </li> <li><span>} </span></li> <li class="alt"><span>echo "serialize读:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_put_contents</span><span>("data/js.php </span> </li> <li class="alt"><span>",json_encode($_SERVER)); </span></li> <li> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_get_contents</span><span>("data/js.php"); </span> </li> <li class="alt"> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">json_decode</span><span>($file); </span> </li> <li><span>} </span></li> <li class="alt"><span>echo "json写:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_put_contents</span><span>("data/js. </span> </li> <li class="alt"><span>php",json_encode($_SERVER)); </span></li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_get_contents</span><span>("data/js.php"); </span> </li> <li class="alt"> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">json_decode</span><span>($file); </span> </li> <li><span>} </span></li> <li class="alt"><span>echo "json读:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li><span>/* </span></li> <li class="alt"> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">var_export</span><span>($_SERVER,1); </span> </li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">""</span><span>; </span> </li> <li class="alt"><span>file_put_contents("data/in.php",$file); </span></li> <li><span>*/ </span></li> <li class="alt"><span>include("data/in.php"); </span></li> <li><span>} </span></li> <li class="alt"><span>echo "include读:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_put_contents</span><span>("data/se. </span> </li> <li class="alt"><span>php",serialize($_SERVER)); </span></li> <li> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_get_contents</span><span>("data/se.php"); </span> </li> <li class="alt"> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">unserialize</span><span>($file); </span> </li> <li><span>} </span></li> <li class="alt"><span>echo "serialize写:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_put_contents</span><span>("data/se. </span> </li> <li class="alt"><span>php",serialize($_SERVER)); </span></li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_get_contents</span><span>("data/se.php"); </span> </li> <li class="alt"> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">unserialize</span><span>($file); </span> </li> <li><span>} </span></li> <li class="alt"><span>echo "serialize读:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_put_contents</span><span>("data/js. </span> </li> <li class="alt"><span>php",json_encode($_SERVER)); </span></li> <li> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_get_contents</span><span>("data/js.php"); </span> </li> <li class="alt"> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">json_decode</span><span>($file); </span> </li> <li><span>} </span></li> <li class="alt"><span>echo "json写:".(microtime(1)-$st)." "; </span></li> <li> <span>$</span><span class="attribute">st</span><span> = </span><span class="attribute-value">microtime</span><span>(1); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">1000</span><span>;$i++){ </span> </li> <li> <span>//$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_put_contents</span><span>("data/js. </span> </li> <li class="alt"><span>php",json_encode($_SERVER)); </span></li> <li> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">file_get_contents</span><span>("data/js.php"); </span> </li> <li class="alt"> <span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">json_decode</span><span>($file); </span> </li> <li><span>} </span></li> <li class="alt"><span>echo "json读:".(microtime(1)-$st)." "; </span></li> </ol>
結果は素晴らしいです! include write: 0.559882879257 include read: 0.185745000839 シリアル化書き込み: 0.255033969879 シリアル化読み取り: 0.0853068828583 json 書き込み: 0.284864902496 json 読み取り: 0.145938873291 読み取りまたは書き込みでは、シリアル化よりも json の 1 倍の 2 倍の効率が初めて得られます。ファイルのキャッシュ効率は若干低くなりますが、パフォーマンスは問題ありません。
ファイルの読み取りと書き込みにかかる時間を除けば、効率の差はさらに大きくなる可能性があります。これは PHP スクリプトの割り当ての形式ですが、テキストの分析と解析も必要です。PHP スクリプト インタプリタでは、インタプリタ全体を使用して PHP スクリプトを分析する必要がありますが、シリアル化ではシリアル化されたテキストを有効にするだけで済みます。分析が行われるため、PHP ファイルのキャッシュがより効率的になります。
http://www.bkjia.com/PHPjc/446094.html
。