note 46042 added to function.microtime
| From: | Jackaloisbackathotmaildotcom at osu1 dot php dot net | Date: | Tue, 28 Sep 2004 07:05:05 +0000 |
| Subject: | note 46042 added to function.microtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-77459@lists.php.net to get a copy of this message | ||
I borrowed the idea from "mark dot young at vdhinc dot com" and improved (from my point of
view) upon it.
This class will allow you to have multiple timers, and should be fairly easy to use.
<?php
class debug_timer
{
var $timers = array();
// Borrowed from PHP4 Documentation
// http://us2.php.net/manual/en/function.microtime.php
function microtime_float ()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
// Adds a timer to the array
function add_timer ($timer_name)
{
$this->timers[$timer_name]['starttime'] = $this->microtime_float();
}
// Ends a timer in the array
function end_timer ($timer_name)
{
$this->timers[$timer_name]['endtime'] = $this->microtime_float();
}
// Outputs code into the page
function output_timer ($timer_name)
{
echo "\n\n<!-- [TIME] ";
echo "Timer: " . $timer_name;
echo " Elapsed: " . number_format( ($this->timers[$timer_name]['endtime']
- $this->timers[$timer_name]['starttime']), 4);
echo " -->\n\n";
}
}
$DEBUG_TIMER = new debug_timer;
// End of class, start of your code
// Start the timer
$DEBUG_TIMER->add_timer("Test Timer");
// Some code or sleep()
sleep(2);
// End the timer
$DEBUG_TIMER->end_timer("Test Timer");
// Output the timer
$DEBUG_TIMER->output_timer("Test Timer");
?>
----
Manual Page -- http://www.php.net/manual/en/function.microtime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46042
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46042&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46042&report=yes
Search -- http://master.php.net/manage/user-notes.php