Forum Moderators: coopster
Is there a way I can pull total page build time out of PHP? Most pages are created from several included scripts and I'm not sure if that's an issue.
Alternatively, I could set a variable to some clocktime in header.php, and subtract that from current time in footer.php, and echo that out.
To do that, is there some php equivilent to C's "tickcount"?
I'm looking for millisecond accuracy, ideally.
Thanks!
TJ
[php.net ]
As you said, obtain start time and stop time as first and last (well almost) lines on the page, and then subtract them
[webmasterworld.com...]
The original poster of the thread probably long forgot this example ;-)
<?php
// PHP5 version:
$time_start = microtime(true);
// Sleep for a while
usleep(1000);
$time_end = microtime(true);
$time = $time_end - $time_start;
printf("<pre>Did nothing in %0.15f seconds\n\n\n</pre>", $time);
// PHP4 version:
function getmicrotime($mt) {
list($msec, $sec) = explode(' ', $mt);
return ((float)$msec + (float)$sec);
}
$time_start = microtime();
for ($i=0; $i < 1000; $i++){
// do nothing, 1000 times
}
$time_end = microtime();
$time_elapsed = getmicrotime($time_end) - getmicrotime($time_start);
printf("Did nothing in %0.15f seconds<br />\n", $time_elapsed);
This page built in 1146919887.045933961868286 seconds
That can't be right ;)
I assume something is wrong in the format statement? Couple of significant digits out. Gut feeling says the figure after the decimal point is about right (1/2 second or so).
microtime -- Returns current Unix timestamp in microseconds
Are you sure that it is in seconds? :)
eelix
this looks like one full microtime timestamp, not a difference between timestamps.
Are you sure you did printf $time_elapsed? Or is your $time_start == 0 for some unknown reason?
You may print out all 3 $time_ values (_start, _end, _elapsed) to debug this.
The "1146919887.x" part looks like the common Unix epoch time in seconds since 1970-01-01 00:00 and translates to 2006-05-06 12:51:27.
Kind regards,
R.
Here's the exact code, snipped as necessary.
Function code:-
function getmicrotime($mt) {
list($msec, $sec) = explode(' ', $mt);
return ((float)$msec + (float)$sec);
}
In the header bit I do this:-
$time_start = microtime();
In the footer I do:-
$time_end = microtime();
$time_elapsed = getmicrotime($time_end) - getmicrotime($time_start);printf("<p>This page built in %0.15f seconds</p>\n", $time_elapsed);
Result is as per my post above - clearly a math error somewhere.
Any thoughts?
I'm using php4.
I'm sure it's simple, but I don't know enough php to spot it...
Thanks ;)
<?php$startTime = array_sum(explode(" ",microtime()));
//php code
$endTime = (array_sum(explode(" ",microtime())) - $startTime);
echo $endTime.' seconds';
?>
Hope this helps
eelix
Bit odd?
Yes, indeed. But what that is telling us is that there is nothing in $time_start. Remember that when microtime() is called without the optional argument (which you can't do unless you are using PHP5 anyway), then the function returns the string "msec sec" where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and msec is the microseconds part. Both portions of the string are returned in units of seconds. So, there is nothing being returned in $time_start, therefore no value displayed, and then the "end: " and $time_end value is displayed.
You don't have $time_start in a function or something do you? Turn up your error_reporting for a bit too, I bet you have an undefined variable error there.
error_reporting [php.net](E_ALL);