最新消息:

直接返回数组的方法是最慢的

PHP admin 4浏览

缓存文件,大家用的最多的估计就是 return array();
实验证明,这种方法是较慢的,试考查以下的3个文件.
其一. $arr.php 直接返回 数组

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文件

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 为序例化后的数组

分别对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 文件不错,方便书写(可以考虑)
保存数组, 用序列化!
相关日志PHP Warning: PHP Startup: pdo_mysql: Unable to initialize module
升级最新 Nginx Mysql Php
PHP关闭浏览器后仍然继续执行的函数
PHP 基于文件头的文件类型验证类
几个有用的php字符串过滤,转换函数

转载请注明:爱开源 » 直接返回数组的方法是最慢的