PHP文件缓存包含三种格式

开发 后端
PHP文件缓存包含有三种格式,我们在文章中做了一下详细的介绍,并对PHP文件缓存的效率做了一个测评,作为大家的参考对象。

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

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文件缓存效率更高。
 

责任编辑:曹凯 来源: 百度博客
相关推荐

2012-08-07 10:02:06

JSP

2017-04-11 15:15:20

CentOSPHP拓展安装

2009-11-17 10:42:58

PHP操作符

2022-03-15 11:31:17

MySQL日志格式

2010-08-26 16:26:19

DB2数据库外部文件

2009-07-29 11:44:30

ASP.NET缓存Cache

2022-05-30 07:07:35

Java监听文件Java 8

2009-12-03 10:26:24

PHP函数strrev

2010-09-02 10:02:17

PHP

2016-10-12 13:53:38

JavaByteBufferRandomAcces

2011-01-18 15:35:59

jQueryJavaScriptweb

2010-09-24 19:18:22

SQL索引

2017-01-20 17:40:12

PHP vs Ruby

2010-02-25 17:01:48

Linux配置文件

2010-08-03 09:20:33

Flex读取XML配置

2022-05-31 16:00:46

Go 编程语言复制文件Go 标准库

2018-07-04 09:19:37

存储类型对象存储

2017-01-20 16:55:13

编程PHPRuby Python

2022-06-20 08:50:16

TypeScript类型语法

2018-03-28 16:10:23

阅读源码境界
点赞
收藏

51CTO技术栈公众号