緩存文件,大家用的最多的估計就是 return array();
實驗證明,這種方法是較慢的,試考查以下的3個文件.
其一. $arr.php 直接返回 數組
<?php
return Array
(
"db_host1" => 123,
"db_host2" => 123,
"db_host3" => 123,
"db_host4" => 123,
"db_host5" => 123,
"db_host6" => 123,
"db_host7" => 123,
"db_host8" => 123,
"db_host9" => 123,
"db_host10" => 123,
"db_host11" => 123,
"db_host12" => 123,
"db_host13" => 123,
"db_host14" => 123,
"db_host15" => 123,
"db_host16" => 123
);
?>
其二, ini.php 為INI文件
<?php exit;?>
db_host1 =123
db_host2 =123
db_host3 =123
db_host4 =123
db_host5 =123
db_host6 =123
db_host7 =123
db_host8 =123
db_host9 =123
db_host10 =123
db_host11 =123
db_host12 =123
db_host13 =123
db_host14 =123
db_host15 =123
db_host16 =123
其三,str.php 為序例化後的數組
<?php
return
a:16:{s:8:"db_host1";s:3:"123";s:8:"db_host2";s:3:"123";s:8:"db_host3";s:3:"123";s:8:"db_host4";s:3:"123";s:8:"db_host5";s:3:"123";s:8:"db_host6";s:3:"123";s:8:"db_host7";s:3:"123";s:8:"db_host8";s:3:"123";s:8:"db_host9";s:3:"123";s:9:"db_host10";s:3:"123";s:9:"db_host11";s:3:"123";s:9:"db_host12";s:3:"123";s:9:"db_host13";s:3:"123";s:9:"db_host14";s:3:"123";s:9:"db_host15";s:3:"123";s:9:"db_host16";s:3:"123";};
?>
分別對3個文件 進行10000次包含返回數組測試
$t1 = microtime(true);
$file1 = ./arr.php;
$file2 = ./ini.php;
$file3 = ./str.php;
for($i=0; $i<10000; $i++){
//$arr = require $file1;
//$arr = parse_ini_file($file2);
$arr = unserialize(require $file3);
}
$t2 = microtime(true);
echo $t2-$t1;
得出結果:
arr.php 5.7820551395416
ini.php 5.3364160060883
str.php 5.5691919326782
其中,排得最快的居然是 INI文件.
其次是序列化, 最慢的是直接 return 數組
結論:
ini 文件不錯,方便書寫(可以考慮)
保存數組, 用序列化!