Page 1 of 1

Timer class

Posted: Tue Dec 18, 2007 6:03 pm
by pickle
I wrote this simple timer class for a project I just finished. Hopefully others find it useful.

PHP5 only!

Code: Select all

<?PHP
  /**********
   * Class: Timer
   * Purpose: Whatever you want - it's just a simple timer class
   * Author: Dylan Anderson
   * Date: December 18, 2007
   * License: GPL
   */
class Timer
{
  private $start;
  private $end;
  private $elapsed;
  private $significant_digits = 2;
 
  function __construct()
  {
    $this->start = microtime(true);
  }
  function resetStart()
  {
    $this->start = microtime(true);
  }
  function end()
  {
    $this->end = microtime(true);
  }
  function elapsed($significant_digits=FALSE)
  {
    $this->end();
    $digits = ($significant_digits) ? $significant_digits : $this->significant_digits;
    $this->elapsed = number_format($this->end - $this->start,$significant_digits);
    return $this->elapsed;
  }
 
  function __get($name)
  {
    switch($name)
    {
      case 'start':
        return $this->start;
      case 'end':
        return $this->end;
      case 'elapsed':
        return $this->elapsed();
    }
  }
}
 
?>

Usage

Page load times:

Code: Select all

<?PHP
$Timer = new Timer();
 
//blah
//blah
//blah
 
echo "Page generated in ".$Timer->elapsed." seconds";
?>

Benchmarking:

Code: Select all

<?PHP
$Timer = new Timer();
 
//For loop test
for($i = 0;$i<100;$i++)
{
   $for_array[] = rand(1,100);
}
$for_time = $Timer->elapsed;
unset($for_array);
 
 
//While loop test
$counter = 0;
while($counter < 100)
{
   $while_array[] = rand(1,100);
   $counter++;
}
$while_time = $Timer->elapsed;
unset($while_array);
 
echo "For loop took $for_time seconds, while loop took $while_time seconds";
?>

Posted: Tue Dec 18, 2007 9:59 pm
by Kieran Huggins
Wow - I wrote this exact thing as a (bizarre) function just the other day... except yours is much cleaner. I don't know what I was on. Oh right, Tylenol 3's. Thank god for subversion :oops:

Thanks! 8)

Posted: Tue Dec 18, 2007 10:26 pm
by s.dot
Looks good.

Seems to me that a call to $this->end() could be eliminated by doing:

Code: Select all

$this->elapsed = number_format(microtime(true) - $this->start,$significant_digits);
And, what's the point of setting a class member and then returning it?

Couldn't you just do:

Code: Select all

return number_format(microtime(true) - $this->start, $significant_digits);
:)

Posted: Wed Dec 19, 2007 10:53 am
by pickle
If the call to $this->end() is removed, then $this->end isn't set. While I don't currently use that variable, it might be useful for someone.

A simple start/stop class could certainly be simpler - but this is more versatile.

Posted: Wed Dec 19, 2007 12:53 pm
by Benjamin
It's a good bit of code. Should be moved to snippets.

Posted: Fri Dec 21, 2007 5:07 pm
by RobertGonzalez
Dude I wrote one for my framework I am developing. Would it be bad form to post it here?

Posted: Sun Dec 23, 2007 8:11 pm
by Kieran Huggins
The more the merrier - sharing is caring.

Posted: Sun Dec 23, 2007 10:09 pm
by s.dot
pickle wrote:If the call to $this->end() is removed, then $this->end isn't set. While I don't currently use that variable, it might be useful for someone.

A simple start/stop class could certainly be simpler - but this is more versatile.
It seems a little backwards including $this->end() inside of the method.

What happens if a user does:

Code: Select all

$timer->end();
echo $timer->elapsed();
They would not get a valid elapsed time. I believe that the elapsed should do exactly that. Return the time elapsed between construct (start) or resetStart(), and end() method calls.

Why magically include end() in the elapsed method?

Coule be a matter of preference, and I am certainly in no position to question your preference :)