Reputation: 3296
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.
Upvotes: 9
Views: 28022
Reputation: 6621
One liner for the footer
, which returns seconds
as float
:
echo number_format(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"],4);
You do not need to put anything at the start of your script, as $_SERVER["REQUEST_TIME_FLOAT"]
takes care to capture this time when the PHP script starts... Then you only need to calculate at the very end of the PHP how much time has passed.
Upvotes: 2
Reputation: 21
Instead of using microtime in every single page, what i would do is to insert microtime into $_REQUEST and then subtract that time from the current time inside a function and set that function to be called when the script execution is terminated using:
register_shutdown_function ( 'Your_function_name' );
I think that it is useful to use a global script which will be included at the beginning of every script / class throughout the application, it helps me in handling errors, managing sessions, etc...
Adding microtime to $_REQUEST would be the first line in that script and you can include your terminating function there too.
Upvotes: 0
Reputation: 73
The issue is with variable scope. You set your $start variable in the header, but in footer, this variable will be empty. So $total_time would just be current time - 0, giving you the current time.
A solution is to use php's GLOBALS. In the header:
$GLOBALS['time_start'] = microtime(true);
And in the footer:
$total_time = round(($finish - $GLOBALS['time_start']), 4);
Upvotes: 0
Reputation: 1
Put this in your Header
<?php
$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];
?>
and this in your footer
<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>
this will tell you how long it took your sites page to generate and how much Memory was used to load the page
Upvotes: 0
Reputation: 25168
microtime()
returns the current Unix timestamp with microseconds. i don't see any math there that does the conversion from microseconds to seconds.
microtime(true)
returns the time as a float in seconds
Upvotes: 6
Reputation: 1010
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:
$startScriptTime=microtime(TRUE);
And then put this code at the bottom of your page:
$endScriptTime=microtime(TRUE);
$totalScriptTime=$endScriptTime-$startScriptTime;
echo "\n\r".'<!-- Load time: '.number_format($totalScriptTime, 4).' seconds -->';
When you view source of a page you can see the load time in a comment on the last line of your HTML.
Upvotes: 8
Reputation: 11
$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>';
Upvotes: 1
Reputation: 3247
You can use this simple function to avoid the variable scope issue:
<?php
function timer()
{
static $start;
if (is_null($start))
{
$start = microtime(true);
}
else
{
$diff = round((microtime(true) - $start), 4);
$start = null;
return $diff;
}
}
timer();
echo 'Page generated in ' . timer() . ' seconds.';
Upvotes: 6
Reputation: 8354
I like to use something like this. Makes it easy to time multiple code blocks without juggling variable names and such. sessions have to be enabled.
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);
}
}
usage:
code_timer ('a');
//do stuff
echo "page generated in " . code_timer('a');
Upvotes: 1