Home > Article > Backend Development > PHP file cache contains three formats_PHP tutorial
There are three main formats for saving PHP file cache content:
1. The variable var_export is formatted into PHP’s normal assignment writing format, when used Directly include the file
2. The variable serialize is saved after serialization, and deserialized when used
3. The variable json_encode is formatted and saved, and json_decode is always used when used. Since then, I have thought that the first type is the most efficient, because it is the format used by the PHP script interpreter to parse PHP scripts. The native one should be the fastest, and at least the efficiency of reading the cache should be the highest. However, I did a test today and found that I was shocked! It turns out that serialize has the highest serialization efficiency, whether it is reading or writing!
Here is the code used to test the PHP file cache:
<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>
The results are amazing! include write: 0.559882879257 include read: 0.185745000839 serialize write: 0.255033969879 serialize read: 0.0853068828583 json write: 0.284864902496 json read: 0.145938873291 Serialization is the fastest, whether reading or writing, it is the first One-times twice as efficient as json than serialization The PHP file caching efficiency is slightly lower, but the performance is okay!
If the time spent reading and writing files is ignored, the difference in their efficiency may be even greater! include, although it is the format of PHP script assignment, but it also needs to analyze and parse the text. The PHP script interpreter needs to use the entire interpreter to analyze the PHP script, but serialization does not need it. Just enable serialized text analysis, so the PHP file Caching is more efficient.
http://www.bkjia.com/PHPjc/446094.html