直接返回數組的方法是最慢的_PHP教程

來源:互聯網
上載者:User

快取檔案,大家用的最多的估計就是 return array();

實驗證明,這種方法是較慢的,試考查以下的3個檔案.

其一. $arr.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檔案


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 為序例化後的數組

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 檔案不錯,方便書寫(可以考慮)

儲存數組, 用序列化!

http://www.bkjia.com/PHPjc/486611.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486611.htmlTechArticle快取檔案,大家用的最多的估計就是 return array(); 實驗證明,這種方法是較慢的,試考查以下的3個檔案. 其一. $arr.php 直接返回 數組 ?php return...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.