Reputation: 105
Is there any way to get a php script that measures the localhost page rendering time. I have looked around but didn't find anything, maybe because I don't know how to search for this kind of script.
Upvotes: 6
Views: 13563
Reputation: 309
Update on original approved answer (for future people 'Googling' and cut-n-paste mojo):
For PHP 5.X+, you not only have to add 'true' as a parameter to both microtime() vars, but we don't have to divide it by 1,000 anymore. So the 2013 answer is:
Start of script:
$start = microtime(true);
Bottom of script:
$end = microtime(true);
$creationtime = ($end - $start);
printf("Page created in %.6f seconds.", $creationtime);
Upvotes: 20
Reputation: 42612
You can't measure the browser rendering time with PHP. All you can do is measure the time it takes the server to generate the page.
However if using javascript is ok, you can do the following:
<html>
<head>
<script type="text/javascript">
var start_time=+new Date();
window.onload=function(){
alert("Page render time: " + ((+new Date()-start_time)/1000));
};
</script>
</head>
<body>
<p>hi</p>
</body>
</html>
If you want a browser plugin, look here: measure page rendering time on IE 6 or FF 3.x
Upvotes: 3
Reputation: 8334
If you're looking to figure out how long it took your server to create the page (for example, to display this to the user), then you'll want to put this at the start of the code:
$start = microtime();
And then at the end put this:
$end = microtime();
$creationtime = ($end - $start) / 1000;
printf("Page created in %.5f seconds.", $creationtime);
Upvotes: 5