PHP file cache contains three formats _php tutorial

Source: Internet
Author: User

PHP file cache Content storage format is mainly three kinds:

1. Variable var_export formatted into PHP normal assignment writing format, when using the direct include file

2. Variable serialize is saved after serialization, and is deserialized when used

3, the variable json_encode after the format of the save, when used Json_decode

All along, I thought the first is the most efficient, because that is the PHP script interpreter parsing PHP script format, the original, should be the fastest, at least the efficiency of reading cache should be the highest, but today did a test, I am surprised! The original serialize serialization efficiency is the highest, whether it is read or write!

The following is the code used to test the PHP file cache:

 
 
  1. View Plaincopy to Clipboardprint?
  2. $ St = Microtime (1);
  3. For ($i=0; $i<$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 + +) {
  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 wrote:". (Microtime (1)-$st). "";
  20. $ St = Microtime (1);
  21. For ($i=0; $i<$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 + +) {
  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 + +) {
  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 + +) {
  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 + +) {
  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 wrote:". (Microtime (1)-$st). "";
  62. $ St = Microtime (1);
  63. For ($i=0; $i<$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 + +) {
  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 + +) {
  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 result is amazing! Include write: 0.559882879257include read: 0.185745000839serialize write: 0.255033969879serialize read: 0.0853068828583json write: 0. 284864902496json read: 0.145938873291 serialization is the fastest, no matter read or write, is the first kind of efficiency of twice times, JSON than the serialized PHP file cache efficiency is slightly lower, performance is also possible!

If you put aside the time it takes to read and write, their efficiency may be more different! Include that, although is the PHP script assignment format, but also to parse the parsing text, the PHP script interpreter needs to use the entire interpreter to parse the PHP script, but the serialization does not need, only then enables the serialization text analysis to be OK, therefore the PHP file cache is more efficient.


http://www.bkjia.com/PHPjc/446094.html www.bkjia.com true http://www.bkjia.com/PHPjc/446094.html techarticle PHP File Cache Content storage format is mainly three kinds: 1. The variable var_export formatted as PHP normal assignment writing format, when used directly include file 2. Variable serialize after serialization ...

  • 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.