当前位置: 代码迷 >> PHP >> php include 跟io流的效率
  详细解决方案

php include 跟io流的效率

热度:77   发布时间:2016-04-28 23:39:51.0
php include 和io流的效率

之前看了,平凡的世界大大的一篇文章 <a href="http://www.ccvita.com/">传送门</a> ,php文件缓存性能测试。很不以为然,include怎么会比io流慢了 ,就算加上序列化,按照常理,include也会稍快一些,但是事实却是是如此! 代码如下

测试io流+序列化

function read_cache($filename) {
??????? if($datas = file_get_contents($filename)){
??????????? return $datas;
??????? }
}
$t1 = gettimeofday();

for ($i = 0; $i < 10000; $i++){
??? $x = read_cache("CacheTest_SerializeData.php");
??? $x_r = unserialize($x);
}

$t2 = gettimeofday();

echo ($t2['sec'] - $t1['sec']) * 1000 + ($t2['usec'] - $t1['usec']) / 1000 . "\n";
测试include

$t1 = gettimeofday();

for ($i = 0; $i < 10000; $i++){
??? include("CacheTest_IncludeData.php");
}

$t2 = gettimeofday();

echo ($t2['sec'] - $t1['sec']) * 1000 + ($t2['usec'] - $t1['usec']) / 1000 . "\n";

结果时间相差1s 但是这中间有一个问题 序列化字符串长度有限制,是基于php数组的限制,所以大存放大量文章内容时这种io+序列化可能达不到预期的需求

  相关解决方案