我认为无论哪种方式,性能都不是一个令人信服的论点。在我的 Mac 上,我运行了以下测试。
前 10,000 个包含一个除了设置变量之外什么都不做的文件:
<?php
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
for ($i = 0; $i < 10000; $i++) {
include("foo.php");
}
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo 'Rendered in ' .$totaltime. ' seconds.';
?>
每次运行大约需要 0.58 秒。 (请记住,包括 10,000 个。)
然后我编写了另一个脚本,查询数据库 10,000 次。它不选择任何真实数据,只选择SELECT NOW()。
<?php
mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('test');
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
for ($i = 0; $i < 10000; $i++) {
mysql_query("select now()");
}
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo 'Rendered in ' .$totaltime. ' seconds.';
?>
这个脚本每次在我的电脑上运行大约需要 0.76 秒。显然,有很多因素可能会影响您的具体情况,但是运行 MySQL 查询与使用包含没有显着的性能差异。 (请注意,我的测试中没有包含 MySQL 连接开销——如果您连接到数据库只是为了获取包含的数据,那会有所不同。)