php include加载文件两种方式效率比较
作者:bea
先来说说两种方式: 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'; // 加载基本系统文件 $kernel_require_files = explode(';', $a);//SYS_REQUIRE_LI
先来说说两种方式:
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';
// 加载基本系统文件
$kernel_require_files = explode(';', $a);//SYS_REQUIRE_LIB_FILE_LIST);
foreach($kernel_require_files as $f){
require_once(SYS_LIB_PATH.'/System'.$f);
}
// 加载基本系统文件
$kernel_require_files = explode(';', $b);//SYS_BASE_FILE_LIST);
foreach($kernel_require_files as $f){
require_once(KERNEL_PATH.$f);
}
2)把所有的要加载的文件都在一个include文件里面加载,当前页直接include这个include文件。
include.php文件内容
代码如下:
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');
我个人认为第二种方法效率高些,因为没有foreach这些多余的运算~凡事要论证,不能凭空想象,所以,我验证了一下。以下是用两种方法随机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
另外,又尝试了一下,直接在当前页面加载所有文件
0.022285938262939
0.024394035339355
0.023194074630737
0.023229122161865
0.024644136428833
0.023538112640381
0.024240016937256
0.025094032287598
0.023231029510498
0.02339506149292
结果令我吃惊啊!竟然第一种貌似最慢的方法,耗时最少,而直接在当前页面加载多个文件耗时也不少啊~
原因?未知啊,希望明眼的给个答案,先不管那么多"X计划"的核心加载部分就用第一种方法啦~
有用 | 无用
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';
// 加载基本系统文件
$kernel_require_files = explode(';', $a);//SYS_REQUIRE_LIB_FILE_LIST);
foreach($kernel_require_files as $f){
require_once(SYS_LIB_PATH.'/System'.$f);
}
// 加载基本系统文件
$kernel_require_files = explode(';', $b);//SYS_BASE_FILE_LIST);
foreach($kernel_require_files as $f){
require_once(KERNEL_PATH.$f);
}
2)把所有的要加载的文件都在一个include文件里面加载,当前页直接include这个include文件。
include.php文件内容
代码如下:
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');
我个人认为第二种方法效率高些,因为没有foreach这些多余的运算~凡事要论证,不能凭空想象,所以,我验证了一下。以下是用两种方法随机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
另外,又尝试了一下,直接在当前页面加载所有文件
0.022285938262939
0.024394035339355
0.023194074630737
0.023229122161865
0.024644136428833
0.023538112640381
0.024240016937256
0.025094032287598
0.023231029510498
0.02339506149292
结果令我吃惊啊!竟然第一种貌似最慢的方法,耗时最少,而直接在当前页面加载多个文件耗时也不少啊~
原因?未知啊,希望明眼的给个答案,先不管那么多"X计划"的核心加载部分就用第一种方法啦~
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- php Notice: Undefined index 错误提示解决方法
- php下统计用户在线时间的一种尝试
- PHP生成excel时单元格内换行问题的解决方法
- PHP下对字符串的递增运算代码
- 深入理解PHP原理之异常机制
- php中var_export与var_dump的区别分析
- php visitFile()遍历指定文件夹函数
- php excel类 phpExcel使用方法介绍
- php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)
- 使用VisualStudio开发php的图文设置方法
- 利用Memcached在php下实现session机制 替换PHP的原生session支持
- ThinkPHP中实例Model方法的区别说明
- PHP 数组基础知识小结
- 一个简单php扩展介绍与开发教程
- php checkbox 取值详细说明
- PHP 截取字符串专题集合
- php 过滤器实现代码
- PHP对字符串的递增运算分析
- PHP原理之异常机制深入分析