PHP 获取页面加载统计信息 - 如何测量 php 脚本执行/加载时间
页眉中的内容:
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
页脚中的内容:
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo 'Page generated in ' . $total_time . ' seconds.';
输出:在 1292008977.54 秒内生成页面。
有人可以帮我弄清楚为什么结果不正确吗?我正在使用PHP5。
What I have in the header:
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
What I have in the footer:
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo 'Page generated in ' . $total_time . ' seconds.';
Output: Page generated in 1292008977.54 seconds.
Can someone please help me figure out why the result is not right?? I am using PHP5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(9)
等数载,海棠开2024-10-14 01:38:32
我喜欢用这样的东西。可以轻松地对多个代码块进行计时,而无需处理变量名称等。必须启用会话。
function code_timer ($name) {
$mtime = explode(' ',microtime());
$time = $mtime[1] + $mtime[0];
//determine if we're starting the timer or ending it
if ($_SESSION["timer-$name"]) {
$stime=$_SESSION["timer-$name"];
unset($_SESSION["timer-$name"]);
return ($time - $stime);
} else {
$_SESSION["timer-$name"]=$time;
return(true);
}
}
用法:
code_timer ('a');
//do stuff
echo "page generated in " . code_timer('a');
从来不烧饼2024-10-14 01:38:32
$page_loadtime_in_millisec = ($page_loadtime / 1000);
echo '<pre>Page Infor:
Page Load Time : ' . $page_loadtime.' <b>Microseconds</b><br/>
Page Load Time : ' . $page_loadtime_in_millisec.' <b>Milliseconds</b><br/>
Page Load Time : ' . number_format(($page_loadtime_in_millisec/1000),18) . ' <b>Seconds</b></pre>';
花期渐远2024-10-14 01:38:32
将其放在页眉中
<?php
$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];
?>
,并将其放在页脚中,
<html><center>Page generated in <?php $load = microtime();print (number_format($load,2));?> seconds. <?php
$loadtime = explode(' ', microtime()); $loadtime = $loadtime[0]+$loadtime[1]-$starttime; echo 'Peak memory usage: ',round(memory_get_peak_usage()/1048576, 2), 'MB';
?></center></html>
这将告诉您生成网站页面需要多长时间以及加载页面使用了多少内存
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
看到这是谷歌的第一个结果,我想我应该分享我对这个问题的解决方案。将其放在页面顶部:
然后将此代码放在页面底部:
当您查看页面源代码时,您可以在 HTML 最后一行的注释中看到加载时间。
Seeing how this is the first result in Google I thought I'd share my solution to this problem. Put this at the top of your page:
And then put this code at the bottom of your page:
When you view source of a page you can see the load time in a comment on the last line of your HTML.