我用microtime(true)
写了一个简单的速度测试脚本。它测试一百万次迭代以下五种包括方法:
// Absolute path.
include('/home/ftpuser/public_html/includes/myscript.php');
// Predefined path.
define('PATH', '/home/ftpuser/public_html/includes/');
include(PATH . 'myscript.php');
// Relative path.
include('myscript.php');
// Using set_include_path().
set_include_path('/home/ftpuser/public_html/includes/');
include('myscript.php');
// Superglobal path.
include(dirname(__FILE__) . '/myscript.php');
哪个结果如下(单位:秒):
Absolute path: 263.222
Predefined path: 263.545
Relative path: 301.214
Using set_include_path(): 302.396
Superglobal path: 269.631
我基于这些结果的意见是使用预定义的路径,因为它是绝对路径中最快的。但是,绝对路径的缺点是必须在每个文件中更改必要的更改。
希望这有助于。 :)
P.S.
define
和set_include_path()
在脚本执行过程中仅使用过一次(它们位于循环之外)。
如果你有'ROOT',为什么不'set_include_path(get_include_path()。PATH_SEPARATOR.ROOT)'? – chelmertz 2009-11-18 11:18:14
因为那么你仍然在搜索包含路径 - 这种方式没有涉及搜索。 – Greg 2009-11-18 12:03:35
如果你先放置它,这是一个非常简单/快速的搜索:'set_include_path(ROOT.PATH_SEPARATOR.get_include_path())'。另外,如果要包含大量文件(可在解答中详细阐述我的意见),可维护性可能会受到影响。) – chelmertz 2009-11-18 12:10:58