php include加載文件兩種方式效率比較
更新時間:2010年08月08日 18:10:48 作者:
這兩天抽了點(diǎn)時間繼續(xù)完善“X計劃”的核心部分,核心嘛,就要加載必須的文件,嘗試了兩種方法,發(fā)現(xiàn)效率是不同的,分享一下吧~
先來說說兩種方式:
1)定義一個字符串變量,里面保存要加載的文件列表。然后foreach加載。
$a = '/a.class.php;/Util/b.class.php;/Util/c.class.php';
$b = '/d.php;/e.class.php;/f.class.php;/g.class.php';
// 加載基本系統(tǒng)文件
$kernel_require_files = explode(';', $a);//SYS_REQUIRE_LIB_FILE_LIST);
foreach($kernel_require_files as $f){
require_once(SYS_LIB_PATH.'/System'.$f);
}
// 加載基本系統(tǒng)文件
$kernel_require_files = explode(';', $b);//SYS_BASE_FILE_LIST);
foreach($kernel_require_files as $f){
require_once(KERNEL_PATH.$f);
}
2)把所有的要加載的文件都在一個include文件里面加載,當(dāng)前頁直接include這個include文件。
include.php文件內(nèi)容
require_once('func.php');
require_once('LangManager.class.php');
require_once('_KernelAutoLoader.class.php');
require_once('ApplicationSettingManager.class.php');
require_once('lib/System/Activator.class.php');
require_once('lib/System/Util/CXML.class.php');
require_once('lib/System/Util/CWeb.class.php');
我個人認(rèn)為第二種方法效率高些,因?yàn)闆]有foreach這些多余的運(yùn)算~凡事要論證,不能憑空想象,所以,我驗(yàn)證了一下。以下是用兩種方法隨機(jī)10次加載所消耗的時間:
foreach
0.017754077911377
0.017686128616333
0.017347097396851
0.018272161483765
0.018272161483765
0.018401145935059
0.018187046051025
0.020787000656128
0.018001079559326
0.017963171005249
include_once('include.php');
0.025792121887207
0.024733066558838
0.025041103363037
0.024915933609009
0.024657011032104
0.024134159088135
0.025845050811768
0.024954080581665
0.024757146835327
0.02684497833252
另外,又嘗試了一下,直接在當(dāng)前頁面加載所有文件
0.022285938262939
0.024394035339355
0.023194074630737
0.023229122161865
0.024644136428833
0.023538112640381
0.024240016937256
0.025094032287598
0.023231029510498
0.02339506149292
結(jié)果令我吃驚??!竟然第一種貌似最慢的方法,耗時最少,而直接在當(dāng)前頁面加載多個文件耗時也不少啊~
原因?未知啊,希望明眼的給個答案,先不管那么多"X計劃"的核心加載部分就用第一種方法啦~
1)定義一個字符串變量,里面保存要加載的文件列表。然后foreach加載。
復(fù)制代碼 代碼如下:
$a = '/a.class.php;/Util/b.class.php;/Util/c.class.php';
$b = '/d.php;/e.class.php;/f.class.php;/g.class.php';
// 加載基本系統(tǒng)文件
$kernel_require_files = explode(';', $a);//SYS_REQUIRE_LIB_FILE_LIST);
foreach($kernel_require_files as $f){
require_once(SYS_LIB_PATH.'/System'.$f);
}
// 加載基本系統(tǒng)文件
$kernel_require_files = explode(';', $b);//SYS_BASE_FILE_LIST);
foreach($kernel_require_files as $f){
require_once(KERNEL_PATH.$f);
}
2)把所有的要加載的文件都在一個include文件里面加載,當(dāng)前頁直接include這個include文件。
include.php文件內(nèi)容
復(fù)制代碼 代碼如下:
require_once('func.php');
require_once('LangManager.class.php');
require_once('_KernelAutoLoader.class.php');
require_once('ApplicationSettingManager.class.php');
require_once('lib/System/Activator.class.php');
require_once('lib/System/Util/CXML.class.php');
require_once('lib/System/Util/CWeb.class.php');
我個人認(rèn)為第二種方法效率高些,因?yàn)闆]有foreach這些多余的運(yùn)算~凡事要論證,不能憑空想象,所以,我驗(yàn)證了一下。以下是用兩種方法隨機(jī)10次加載所消耗的時間:
foreach
0.017754077911377
0.017686128616333
0.017347097396851
0.018272161483765
0.018272161483765
0.018401145935059
0.018187046051025
0.020787000656128
0.018001079559326
0.017963171005249
include_once('include.php');
0.025792121887207
0.024733066558838
0.025041103363037
0.024915933609009
0.024657011032104
0.024134159088135
0.025845050811768
0.024954080581665
0.024757146835327
0.02684497833252
另外,又嘗試了一下,直接在當(dāng)前頁面加載所有文件
0.022285938262939
0.024394035339355
0.023194074630737
0.023229122161865
0.024644136428833
0.023538112640381
0.024240016937256
0.025094032287598
0.023231029510498
0.02339506149292
結(jié)果令我吃驚??!竟然第一種貌似最慢的方法,耗時最少,而直接在當(dāng)前頁面加載多個文件耗時也不少啊~
原因?未知啊,希望明眼的給個答案,先不管那么多"X計劃"的核心加載部分就用第一種方法啦~
相關(guān)文章
PHP使用new StdClass()創(chuàng)建空對象的方法分析
這篇文章主要介紹了PHP使用new StdClass()創(chuàng)建空對象的方法,結(jié)合具體實(shí)例形式分析了php空對象的創(chuàng)建與使用方法,需要的朋友可以參考下2017-06-06PHP實(shí)現(xiàn)二維數(shù)組根據(jù)key進(jìn)行排序的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)二維數(shù)組根據(jù)key進(jìn)行排序的方法,涉及php數(shù)組的遍歷與排序相關(guān)操作技巧,需要的朋友可以參考下2016-12-12PHP擴(kuò)展mcrypt實(shí)現(xiàn)的AES加密功能示例
這篇文章主要介紹了PHP擴(kuò)展mcrypt實(shí)現(xiàn)的AES加密功能,結(jié)合實(shí)例形式分析了php基于mcrypt實(shí)現(xiàn)AES加密的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01PHP編碼規(guī)范-php coding standard
標(biāo)準(zhǔn)化問題在某些方面上讓每個人頭痛,讓人人都覺得大家處于同樣的境地。這有助于讓這些建議在許多的項目中不斷演進(jìn),許多公司花費(fèi)了許多星期逐子字逐句的進(jìn)行爭論。2007-03-03