Click to See Complete Forum and Search --> : Class - Timer


ShawnK
03-05-2005, 06:53 PM
I know there is probaly hundreds of these but oh well. I like mine better! :p


<?php

/*
Timer Class
Copyright (c) 2005 - Shawn Kurtz
shawnkurtz[at]gmail.com
================================
There are two(2) parameters for
the constructor. Both are optional.
The first allows you to use your own
text for the output. By default it is:

Page Generated in: __TIME__ seconds

You can specify your own text, just put
__TIME__ where you want the time to go.

The second parameter is the number of
digits you would like it to round to.
The default is 5 digits.
*/

class timer {

function __construct( $funcFormat = "Page Generated in: __TIME__ seconds" , $funcRoundTo = 5)
{
$this->format = $funcFormat;
$this->roundTo = $funcRoundTo;
$this->start( );
}

function start( )
{
$this->startTime = microtime( );
$this->startTime = explode(" ", $this->startTime);
$this->startTime = $this->startTime[0] + $this->startTime[1];
}

function stop( )
{
$this->endTime = microtime( );
$this->endTime = explode(" ", $this->endTime );
$this->endTime = $this->endTime[0] + $this->endTime[1];
$this->totalTime = $this->endTime - $this->startTime;
$this->totalTime = round( $this->totalTime , $this->roundTo );
}

function show( )
{
$this->stop( );
$this->output = str_replace("__TIME__", $this->totalTime, $this->format);
echo $this->output;
}
}

?>

bubblenut
03-05-2005, 08:31 PM
Hello mate, don't seem to see much of you around the rest of the forums, but anyway, this is quickly becoming my haunt of choice so as long as you hang around here I'm happy :)

I've only got a couple of points.
1. You don't seem to have implemtned your class variables, which I'm sure is a mis-paste because the class won't run without them.
2. You're using the PHP5 syntax for the constructor but you're not stating whether or not your methods are public, private or protected. I know it's just a technicality but if the features there we may as well get in the habit of using it :)
3. I'm not sure if PHP5 manages big ints differently to PHP4 but I know that you used to have to use bcmath for the level of subtraction (and maths in general) that you're doing in this class.
4. If you're going to be using this for profiling, as I imagine you are, how would you manage multiple timers running at the same time? It might be quite nice to have the ability to handle more than one timer in the same class or prehapse have another class which manages a collection of timers. This may be getting a little too patterned for the requirements of the class but it's always nice to think about how it could be extended. :)

Weedpacket
03-05-2005, 09:55 PM
I've only looked at this long enough to make one observation: since this is PHP5, the first three lines of start() and stop() can be reduced to one by using microtime(true).

ShawnK
03-06-2005, 02:37 PM
What do you mean weedpacket?

revised code:

<?php

/*
Timer Class
Copyright (c) 2005 - Shawn Kurtz
shawnkurtz[at]gmail.com
================================
There are two(2) parameters for
the constructor. Both are optional.
The first allows you to use your own
text for the output. By default it is:

Page Generated in: __TIME__ seconds

You can specify your own text, just put
__TIME__ where you want the time to go.

The second parameter is the number of
digits you would like it to round to.
The default is 5 digits.
*/

class timer {

private $format;
private $roundTo;
private $startTime;
private $endTime;
private $totalTime;
private $output;

public function __construct( $funcFormat = "Page Generated in: __TIME__ seconds" , $funcRoundTo = 5)
{
$this->format = $funcFormat;
$this->roundTo = $funcRoundTo;
$this->start( );
}

private function start( )
{
$this->startTime = microtime( );
$this->startTime = explode(" ", $this->startTime);
$this->startTime = $this->startTime[0] + $this->startTime[1];
}

private function stop( )
{
$this->endTime = microtime( );
$this->endTime = explode(" ", $this->endTime );
$this->endTime = $this->endTime[0] + $this->endTime[1];
$this->totalTime = $this->endTime - $this->startTime;
$this->totalTime = round( $this->totalTime , $this->roundTo );
}

public function show( )
{
$this->stop( );
$this->output = str_replace("__TIME__", $this->totalTime, $this->format);
echo $this->output;
}
}
?>

Weedpacket
03-06-2005, 09:36 PM
microtime
When get_as_float is given, and evaluates to TRUE, microtime() will return a float.

Note: The get_as_float parameter was added as of PHP 5.0.0.

ShawnK
03-06-2005, 09:57 PM
A quick question, because I am using this in a large-scale project should I worry about checking to see if it's PHP5, and if it is use one thing, if <5 then use something else? Or should I leave the code as I have it?

Weedpacket
03-07-2005, 06:09 AM
Originally posted by ShawnK
A quick question, because I am using this in a large-scale project should I worry about checking to see if it's PHP5, and if it is use one thing, if <5 then use something else? Or should I leave the code as I have it? Well, your code won't run on anything less than 5, so if you want it to, then yes :). By keeping the same names and API for v.4 and v.5 classes, and by putting all the version-specific stuff in separate files, you can select the appropriate edition at runtime with


if(version_compare(phpversion(),'5.0.0')<0)
require_once('PHP4classes.php');
else
require_once('PHP5classes.php');