Re: PHP Execution Timer
| From: | Chris Shiflett | Date: | Thu, 19 Sep 2002 22:44:33 +0000 |
| Subject: | Re: PHP Execution Timer | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-116939@lists.php.net to get a copy of this message | ||
Here is a function I sometimes use to log that will show you the time that has passed since the last log entry. I find it very useful, so that you can test a section of code's execution time simply by surrounding it with calls to this function.
Maybe it will be useful to you.
Chris
function log_message($log_file, $log_message)
{
global $last_log_write;
# SET FORMATTED DATE
$formatted_date=date("Y-m-d H:i:s");
# IF THIS IS THE FIRST LOG MESSAGE
if (!isset($last_log_write))
{
error_log("[$formatted_date] [-----------------] $log_message\n", "3", $log_file);
$last_log_write=microtime();
$last_log_write=explode(" ", $last_log_write);
$last_log_write=doubleval($last_log_write["1"]) + doubleval($last_log_write["0"]);
}
# ELSE (THIS IS NOT THE FIRST MESSAGE), CALCULATE TIME SINCE LAST LOG
else
{
$curr_log_write=microtime();
$curr_log_write=explode(" ", $curr_log_write);
$curr_log_write=doubleval($curr_log_write["1"]) + doubleval($curr_log_write["0"]);
$elapsed_time=$curr_log_write-$last_log_write;
$elapsed_time=substr($elapsed_time, "0", "8");
$log_message=str_replace("\r", "", $log_message);
$log_message=str_replace("\n", "\n ", $log_message);
error_log("[$formatted_date] [+$elapsed_time seconds] $log_message\n", "3", $log_file);
$last_log_write=$curr_log_write;
}
}
?>
-=[ Julien Bonastre ]=- wrote:
The idea is a page "execution" timer.. or a database query timer.. basically it shows you just a time (usually in msecs) it took for the last command to be executed.. I have seen it used for queries and it returns the time it took.. And I am quite sure I've seen it used for actual page generation as well, whereby it says something like "page generated in x.xxx secs"