我有兩個頁面:哪一個更快?單獨的HTML還是嵌入在PHP中?
的index.php:
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$text = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
html content
</body>
</html>';
echo $text;
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This page was created in ".$totaltime." seconds";
?>
和index2.php:
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo 'html content'; ?>
</body>
</html>
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This page was created in ".$totaltime." seconds";
?>
的index.php的測試結果:
html content This page was created in 6.1988830566406E-5 seconds
和index2.php:
html contentThis page was created in 6.4849853515625E-5 seconds
爲什麼嵌入PHP的HTML比index2.php更快?
的index.php是非常非常無用 – Eray 2011-04-03 20:09:42
這不是測量程序的性能,除非你這個平均超過數千奔跑的正確方法。即使如此,由於您生成的HTML是微不足道的,所以它不太可能重要。 – 2011-04-03 20:09:44
這裏看不到太多的速度 – fazo 2011-04-03 20:09:54