PHP simple timer class

2007-09-17

This simple class is useful to benchmark different parts of your php code.

Here is a simple example on how to use the class

include('timer.inc')l
$for_timer = new timer();//initialize timer
//execute
$j = 100;
for ( $i = 1; $i< 10000; $i++)
{
    $j = $i * $j
}
echo $timer -> end();//display how many milliseconds the "for" took.
$while_timer = new timer();
$j = 100;
$i = 1;
while ($i < 10000) 
{
    $j = $i * $j
    $i++;
}
echo $timer -> end();//display how many milliseconds the "while" took.

And here is the class

/*
    Timer class
    Copyright (C) 2007 CodeAssembly.com  

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses/
*/class timer
{
    private $starttime;

    function __construct()
    {
    	$this -> start();
    	//call start automatically
    }

	//start (if you want to "restart" benchmarking)
    function start()
    {

   		$mtime = microtime();
	    $mtime = explode(' ',$mtime);
	    $mtime = $mtime[1] + $mtime[0];
	    $this ->  starttime = $mtime;
    }

    //returns time taken from "start" or class initialization to calling this method
    function end()
    {
        $mtime = microtime();
        $mtime = explode(' ',$mtime);
        $mtime = $mtime[1] + $mtime[0];
        return ($mtime - $this -> starttime);
    }
}

Share this with the world

Related

Comments

No comments at this time

Make yourself heard

Categories

Subscribe

All Posts

Php posts

All Comments

This post comments

© Copyright CodeAssembly

All code is licensed under GPL, unless otherwise noted