Timer class

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Timer class

Post 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";
?>
Last edited by pickle on Fri Apr 11, 2008 10:29 am, edited 1 time in total.
Reason: Updating to [code] tags from [syntax]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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);
:)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

It's a good bit of code. Should be moved to snippets.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Dude I wrote one for my framework I am developing. Would it be bad form to post it here?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

The more the merrier - sharing is caring.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply