note 46042 deleted from function.microtime by aidan
| From: | aidan@php.net | Date: | Tue, 28 Sep 2004 12:09:01 +0000 |
| Subject: | note 46042 deleted from function.microtime by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77486@lists.php.net to get a copy of this message | ||
Note Submitter: Jackaloisback at hotmail dot com
----
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");
?>