PHP头条
热点:

PHP文件缓存包含三种格式


PHP文件缓存的速度一直是PHP程序员们关心的问题,他们一直在探讨着如何才能提高PHP文件缓存的效率来满足自己的开发需求。

  • 如何正确运用PHP json_encode函数进行中
  • 分享PHP数组变成JSON数组的技巧
  • 如何实现centos环境中的PHP JSON安装
  • 详细介绍PHP JSON类库应用范例
  • 技巧总结 正确使用PHP JSON扩展

PHP文件缓存内容保存格式主要有三种:

1.变量 var_export 格式化成PHP正常的赋值书写格式,用的时候直接include文件

2.变量 serialize 序列化之后保存,用的时候反序列化

3,变量 json_encode格式化之后保存,用的时候json_decode

一直以来,我都以为第一种效率最高,因为那是PHP脚本解释器解析PHP脚本的格式,原生的,应该最快,至少读取缓存的效率应该是最高的,可是今天做了个测试,令我大跌眼镜!原来 serialize序列化效率才是最高的,不论是读还是写!

下面是用来测试的PHP文件缓存的代码:

  1. view plaincopy to clipboardprint?    
  2. $st = microtime(1);     
  3. for ($i=0;$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读:".(microtime(1)-$st)." ";     
  12. $st = microtime(1);     
  13. for ($i=0;$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写:".(microtime(1)-$st)." ";     
  20. $st = microtime(1);     
  21. for ($i=0;$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读:".(microtime(1)-$st)." ";     
  28. $st = microtime(1);     
  29. for ($i=0;$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写:".(microtime(1)-$st)." ";     
  36. $st = microtime(1);     
  37. for ($i=0;$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读:".(microtime(1)-$st)." ";     
  44. $st = microtime(1);    
  45. for ($i=0;$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读:".(microtime(1)-$st)." ";    
  54. $st = microtime(1);    
  55. for ($i=0;$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写:".(microtime(1)-$st)." ";    
  62. $st = microtime(1);    
  63. for ($i=0;$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读:".(microtime(1)-$st)." ";    
  70. $st = microtime(1);    
  71. for ($i=0;$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写:".(microtime(1)-$st)." ";    
  78. $st = microtime(1);    
  79. for ($i=0;$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读:".(microtime(1)-$st)." ";  

结果太神奇了!include写:0.559882879257include读:0.185745000839serialize写:0.255033969879serialize读:0.0853068828583json写:0.284864902496json读:0.145938873291 序列化是最快,无论读或写,都是第一种的效率的两倍,json比序列化的PHP文件缓存效率稍低,表现还可以!

如果撇开文件读写所耗费的时间,他们的效率差别可能会更大!include那个,虽然是PHP脚本赋值的格式,但是也是要分析解析文本,PHP脚本解释器需要动用整个解释器分析PHP脚本,而序列化不需要,只用启用序列化文本分析就行了,所以PHP文件缓存效率更高。
 

www.phpzy.comtrue/php/12741.htmlTechArticlePHP文件缓存包含三种格式 PHP文件缓存的速度一直是PHP程序员们关心的问题,他们一直在探讨着如何才能提高PHP文件缓存的效率来满足自己的开发需求。 如何正确运用PHP json_encode函数进...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐