There are three main formats for saving PHP File Cache content:
1. The variable var_export is formatted as a normal PHP value assignment format. When used, the file is directly included.
2. The variable serialize is saved after serialization and deserialized when used.
3. format the variable json_encode and save it. When used, json_decode
I have always thought that the first method is the most efficient, because it is the PHP script interpreter that parses the PHP script format. The native method should be the fastest, and at least the cache reading efficiency should be the highest, but today I did a test, which surprised me! Originally, serialize serialization efficiency was the highest, whether it was read or write!
The following code is used to test the PHP File Cache:
- View plaincopy to clipboardprint?
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- /*
- $ File = var_export ($ _ SERVER, 1 );
- $ File = "";
- File_put_contents ("data/in. php", $ file );
- */
- Include ("data/in. php ");
- }
- Echo "include read:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- $ File = file_put_contents ("data/se. php"
- , Serialize ($ _ SERVER ));
- // $ File = file_get_contents ("data/se. php ");
- // $ File = unserialize ($ file );
- }
- Echo "serialize write:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- // $ File = file_put_contents ("data/se.
- Php ", serialize ($ _ SERVER ));
- $ File = file_get_contents ("data/se. php ");
- $ File = unserialize ($ file );
- }
- Echo "serialize read:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- $ File = file_put_contents ("data/js. php
- ", Json_encode ($ _ SERVER ));
- // $ File = file_get_contents ("data/js. php ");
- // $ File = json_decode ($ file );
- }
- Echo "json write:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- // $ File = file_put_contents ("data/js.
- Php ", json_encode ($ _ SERVER ));
- $ File = file_get_contents ("data/js. php ");
- $ File = json_decode ($ file );
- }
- Echo "json read:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- /*
- $ File = var_export ($ _ SERVER, 1 );
- $ File = "";
- File_put_contents ("data/in. php", $ file );
- */
- Include ("data/in. php ");
- }
- Echo "include read:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- $ File = file_put_contents ("data/se.
- Php ", serialize ($ _ SERVER ));
- // $ File = file_get_contents ("data/se. php ");
- // $ File = unserialize ($ file );
- }
- Echo "serialize write:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- // $ File = file_put_contents ("data/se.
- Php ", serialize ($ _ SERVER ));
- $ File = file_get_contents ("data/se. php ");
- $ File = unserialize ($ file );
- }
- Echo "serialize read:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- $ File = file_put_contents ("data/js.
- Php ", json_encode ($ _ SERVER ));
- // $ File = file_get_contents ("data/js. php ");
- // $ File = json_decode ($ file );
- }
- Echo "json write:". (microtime (1)-$ st )."";
- $ St = microtime (1 );
- For ($ I = 0; I I <1000; $ I ++ ){
- // $ File = file_put_contents ("data/js.
- Php ", json_encode ($ _ SERVER ));
- $ File = file_get_contents ("data/js. php ");
- $ File = json_decode ($ file );
- }
- Echo "json read:". (microtime (1)-$ st )."";
The results are amazing! Include write: 0.559882879257include read: 0.185745000839serialize write: 0.25503369879serialize read: bytes write: 0.284864902496json read: 0.145938873291 serialization is the fastest. No matter whether it is read or write, it is the first, json is slightly less efficient than the serialized PHP File Cache, and the performance is good!
If the time it takes to read and write files, their efficiency may be significantly different! Include, which is in the format of PHP script assignment, but also needs to analyze the parsing text. The PHP script interpreter needs to use the whole interpreter to analyze the PHP script, and serialization is not required, you only need to enable serialized text analysis, so PHP file caching is more efficient.