我使用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');
这给出了以下结果(以秒为单位):
绝对路径:263.222
预定义路径:263.545
相对路径:301.214
使用 set_include_path(): 302.396
超全局路径:269.631
My opinion based on these results is to use a predefined path, because it's the fastest only surpassed by an absolute path. However, an absolute path has the drawback that it must be altered in every file when a change is necessary.
Hope this helped. :)
P.S.
define
and set_include_path()
were used only once during execution of the script (they are located outside of the loop).