自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP文件緩存包含三種格式

開發(fā) 后端
PHP文件緩存包含有三種格式,我們在文章中做了一下詳細(xì)的介紹,并對PHP文件緩存的效率做了一個測評,作為大家的參考對象。

PHP文件緩存的速度一直是PHP程序員們關(guān)心的問題,他們一直在探討著如何才能提高PHP文件緩存的效率來滿足自己的開發(fā)需求。#t#

PHP文件緩存內(nèi)容保存格式主要有三種:

1.變量 var_export 格式化成PHP正常的賦值書寫格式,用的時候直接include文件

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

3,變量 json_encode格式化之后保存,用的時候json_decode

一直以來,我都以為第一種效率最高,因為那是PHP腳本解釋器解析PHP腳本的格式,原生的,應(yīng)該最快,至少讀取緩存的效率應(yīng)該是最高的,可是今天做了個測試,令我大跌眼鏡!原來 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)." ";  

 

結(jié)果太神奇了!include寫:0.559882879257include讀:0.185745000839serialize寫:0.255033969879serialize讀:0.0853068828583json寫:0.284864902496json讀:0.145938873291 序列化是最快,無論讀或?qū)懀际堑谝环N的效率的兩倍,json比序列化的PHP文件緩存效率稍低,表現(xiàn)還可以!

如果撇開文件讀寫所耗費的時間,他們的效率差別可能會更大!include那個,雖然是PHP腳本賦值的格式,但是也是要分析解析文本,PHP腳本解釋器需要動用整個解釋器分析PHP腳本,而序列化不需要,只用啟用序列化文本分析就行了,所以PHP文件緩存效率更高。
 

責(zé)任編輯:曹凱 來源: 百度博客
相關(guān)推薦

2012-08-07 10:02:06

JSP

2024-07-01 12:42:58

2017-04-11 15:15:20

CentOSPHP拓展安裝

2009-11-17 10:42:58

PHP操作符

2022-03-15 11:31:17

MySQL日志格式

2022-05-30 07:07:35

Java監(jiān)聽文件Java 8

2010-08-26 16:26:19

DB2數(shù)據(jù)庫外部文件

2011-01-18 15:35:59

jQueryJavaScriptweb

2010-09-24 19:18:22

SQL索引

2009-07-29 11:44:30

ASP.NET緩存Cache

2010-09-02 10:02:17

PHP

2009-12-03 10:26:24

PHP函數(shù)strrev

2016-10-12 13:53:38

JavaByteBufferRandomAcces

2018-07-04 09:19:37

存儲類型對象存儲

2022-06-20 08:50:16

TypeScript類型語法

2010-10-28 10:27:35

oracle賦權(quán)

2010-09-25 14:38:29

SQL分頁

2009-07-16 16:23:59

Swing線程

2017-01-20 17:40:12

PHP vs Ruby

2017-01-20 16:55:13

編程PHPRuby Python
點贊
收藏

51CTO技術(shù)棧公眾號