PHP中include与include_once加载文件性能测试

PHP中include与include_once加载文件性能测试

1.用include_once加载文件

function test_include(){

$time_start=microtime_float();

for($i=0;$i<100;$i++){

include_once('py_dic.php');

}

$time_end=microtime_float();

printf("total time:%s%s",$time_end-$time_start,l);

}

test_include();

循环100次,用include_once

total time:0.037981986999512

2.用include加载文件

function test_include(){

$time_start=microtime_float();

for($i=0;$i<100;$i++){

include('py_dic.php');

}

$time_end=microtime_float();

printf("total time:%s%s",$time_end-$time_start,l);

}

test_include();

循环100次,用include

total time:3.7502920627594

3.总结

用include_once加载文件时同样的文件只加载一次,所以速度比较快,而如果用include,则需要每次加载同一个文件,增加了IO操作,所以速度就慢一些,用include_once的速度大概是用include的速度的98位,3.7502920627594/0.037981986999512=98.7387011。