简单人生
幻想指点江山,梦中激扬文字

     很多朋友问我,为什么我写的程序和别人写的程序相比,效率没有别人的高?为什么我的执行时间和他们相比总是高出很多?我在检查了他们写的程序后发现,抛开算法的因素,他们写的程序却并不比其他人差很多,可是……为什么效率却相差那么多呢?(这里我不谈数据库缓存和模板及其他缓存技术
    于是我检查了一下他的common文件,发现他include和Require了很多文件,问他为什么这样,他回答说这样的话,我在开发的时候不会遇到哪个文件没有加载而要再临时加载了。于是我回答他,那么你知道这个每一次的include和require产生的IO的消耗如何计算?很多文件,你完全可以等到真正需要的时候再加载也可以啊。为什么非要在一开始就把所有的文件include进来?朋友回答说,这样的临时加载不错,可是太烦了,而且我现在都已经这样了,我怎么知道该如何才能知道该加载哪些文件不该加载哪些文件呢?
    我翻开手册,指了一个函数,说,就是它了。它就是:get_included_files

get_included_files
(PHP 4, PHP 5)

get_included_files --  Returns an array with the names of included or required files 
Description
array get_included_files ( void )


Returns an array of the names of all files that have been included using include(), include_once(), require() or require_once(). 

The script originally called is considered an "included file," so it will be listed together with the files referenced by include() and family. 

Files that are included or required multiple times only show up once in the returned array. 

注: Files included using the auto_prepend_file configuration directive are not included in the returned array. 

例子 1. get_included_files() example (abc.php)

<? php

include 'test1.php';
include_once 'test2.php';
require 'test3.php';
require_once 'test4.php';

$included_files = get_included_files();

foreach ($included_files as $filename) {
    echo "$filename\n";
}

?>   

will generate the following output: 

abc.php
test1.php
test2.php
test3.php
test4.php
 
 


注: In PHP 4.0.1pl2 and previous versions get_included_files() assumed that the required files ended in the extension .php; other extensions would not be returned. The array returned by get_included_files() was an associative array and only listed files included by include() and include_once(). 

See also include(), include_once(), require(), require_once(), and get_required_files(). 

    因此,在PHP文件的结束时,用print_r打印出哪些文件是可以不需加载的,可以在common文件中取消掉,可以提高不少速度。特别是一些比较大的类文件,可以节约很多哦。
    当然这也只是一家之言,其实在写程序的时候,大家都知道哪些类库是一开始就要加载,一开始不需要加载的,比如分页类,这种类文件到用的时候再加载都来得及,而不需要一开始就放到common文件里。
    希望文章可以给大家带来一点小小的帮助。

posted on 2007-01-21 20:25 简单人生 阅读(1010) 评论(0)  编辑 收藏 引用 所属分类: Loving PHP
只有注册用户登录后才能发表评论。