PHP File Cache contains three formats

Source: Internet
Author: User

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:

 
 
  1. View plaincopy to clipboardprint?
  2. $ St = microtime (1 );
  3. For ($ I = 0; I I <1000; $ I ++ ){
  4. /*
  5. $ File = var_export ($ _ SERVER, 1 );
  6. $ File = "";
  7. File_put_contents ("data/in. php", $ file );
  8. */
  9. Include ("data/in. php ");
  10. }
  11. Echo "include read:". (microtime (1)-$ st )."";
  12. $ St = microtime (1 );
  13. For ($ I = 0; I I <1000; $ I ++ ){
  14. $ File = file_put_contents ("data/se. php"
  15. , Serialize ($ _ SERVER ));
  16. // $ File = file_get_contents ("data/se. php ");
  17. // $ File = unserialize ($ file );
  18. }
  19. Echo "serialize write:". (microtime (1)-$ st )."";
  20. $ St = microtime (1 );
  21. For ($ I = 0; I I <1000; $ I ++ ){
  22. // $ File = file_put_contents ("data/se.
  23. Php ", serialize ($ _ SERVER ));
  24. $ File = file_get_contents ("data/se. php ");
  25. $ File = unserialize ($ file );
  26. }
  27. Echo "serialize read:". (microtime (1)-$ st )."";
  28. $ St = microtime (1 );
  29. For ($ I = 0; I I <1000; $ I ++ ){
  30. $ File = file_put_contents ("data/js. php
  31. ", Json_encode ($ _ SERVER ));
  32. // $ File = file_get_contents ("data/js. php ");
  33. // $ File = json_decode ($ file );
  34. }
  35. Echo "json write:". (microtime (1)-$ st )."";
  36. $ St = microtime (1 );
  37. For ($ I = 0; I I <1000; $ I ++ ){
  38. // $ File = file_put_contents ("data/js.
  39. Php ", json_encode ($ _ SERVER ));
  40. $ File = file_get_contents ("data/js. php ");
  41. $ File = json_decode ($ file );
  42. }
  43. Echo "json read:". (microtime (1)-$ st )."";
  44. $ St = microtime (1 );
  45. For ($ I = 0; I I <1000; $ I ++ ){
  46. /*
  47. $ File = var_export ($ _ SERVER, 1 );
  48. $ File = "";
  49. File_put_contents ("data/in. php", $ file );
  50. */
  51. Include ("data/in. php ");
  52. }
  53. Echo "include read:". (microtime (1)-$ st )."";
  54. $ St = microtime (1 );
  55. For ($ I = 0; I I <1000; $ I ++ ){
  56. $ File = file_put_contents ("data/se.
  57. Php ", serialize ($ _ SERVER ));
  58. // $ File = file_get_contents ("data/se. php ");
  59. // $ File = unserialize ($ file );
  60. }
  61. Echo "serialize write:". (microtime (1)-$ st )."";
  62. $ St = microtime (1 );
  63. For ($ I = 0; I I <1000; $ I ++ ){
  64. // $ File = file_put_contents ("data/se.
  65. Php ", serialize ($ _ SERVER ));
  66. $ File = file_get_contents ("data/se. php ");
  67. $ File = unserialize ($ file );
  68. }
  69. Echo "serialize read:". (microtime (1)-$ st )."";
  70. $ St = microtime (1 );
  71. For ($ I = 0; I I <1000; $ I ++ ){
  72. $ File = file_put_contents ("data/js.
  73. Php ", json_encode ($ _ SERVER ));
  74. // $ File = file_get_contents ("data/js. php ");
  75. // $ File = json_decode ($ file );
  76. }
  77. Echo "json write:". (microtime (1)-$ st )."";
  78. $ St = microtime (1 );
  79. For ($ I = 0; I I <1000; $ I ++ ){
  80. // $ File = file_put_contents ("data/js.
  81. Php ", json_encode ($ _ SERVER ));
  82. $ File = file_get_contents ("data/js. php ");
  83. $ File = json_decode ($ file );
  84. }
  85. 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.
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.