Home >Backend Development >PHP Tutorial > php include 跟io流的效率
php include 和io流的效率
之前看了,平凡的世界大大的一篇文章 传送门 ,php文件缓存性能测试。很不以为然,include怎么会比io流慢了 ,就算加上序列化,按照常理,include也会稍快一些,但是事实却是是如此! 代码如下
测试io流+序列化
function read_cache($filename) {
??????? if($datas = file_get_contents($filename)){
??????????? return $datas;
??????? }
}
$t1 = gettimeofday();
for ($i = 0; $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 ??? include("CacheTest_IncludeData.php");
}
$t2 = gettimeofday();
echo ($t2['sec'] - $t1['sec']) * 1000 + ($t2['usec'] - $t1['usec']) / 1000 . "\n";
结果时间相差1s 但是这中间有一个问题 序列化字符串长度有限制,是基于php数组的限制,所以大存放大量文章内容时这种io+序列化可能达不到预期的需求