Zeroth
Zeroth

Reputation: 83

How do we know script' execution time ? OOP

I would love to know if this script is good to know the execution of php script ?

for miliseconds

<?php
$timestart = microtime(true);
/* Blah Blah here ... */
$timeend = microtime(true);
echo 'Execution Time : '.round((timeend - timestart) * 1000, 2);
?>

I have no ideas about using OOP (object-oriented programming) with it.

Also I'll make a script who will parse a text files (.txt), I'll have maybe 120 - 700 lines, which way is better to know the data treatment ?

Does the time depend on number of lines?

Upvotes: 2

Views: 313

Answers (2)

roselan
roselan

Reputation: 3775

I use this Timer class i wrote some time ago. An advantage is that it's "incremental" you can start a timer inside a loop, and it will append the time between start and stop.

Please note that if you do that, it will had quite some time to execution. basic usage:

$myTimer = new Timer();
$myTimer->start('hello');  // $myTimer = new Timer('hello'); is a shorthand for this.
for ($i=0; $i<100000; $i++)
{
  $myTimer->start('slow suspect 1');
  // stuff
  $myTimer->stop('slow suspect 1');    

  $myTimer->start('slow suspect 2');
  // moar stuff
  $myTimer->stop('slow suspect 2');    
}
$myTimer->stop('hello');
$myTimer->print_all();

please note it's limited and far not the fastest way to do it. creating and destoying classes takes time. When done inside "logical" loops, it can add quite some time. but to trace some complex program with multiple imbricated loops, or recursive functions calls, this stuff is precious

<?php
class Timer_
{
    public $start;
    public $stop;
    public function __construct()
    {
        $this->start = microtime(true);
    }
}

class Timer
{
    private $timers = array();

    public function __construct($firsTimer=null)
    {
        if ( $firsTimer != null) $this->timers[$firsTimer][] = new Timer_();
    }

    public function start($name)
    {
        $this->timers[$name][] = new Timer_();  
    }

    public function stop($name)
    {
        $pos = count($this->timers[$name]) -1 ; 
        $this->timers[$name][$pos]->stop = microtime(true); 
    }

    public function print_all($html=true)
    {
        if ( $html ) echo '<pre>';

        foreach ($this->timers as $name => $timerArray)
        {
            $this->print_($name, $html);
        }

        if ( $html ) echo '</pre>';
    }

    public function print_($name, $html=true)
    {
        $nl = ( $html ) ? '<br>' : NEWLINE;
        $timerTotal = 0; 
        foreach ($this->timers[$name] as $key => $timer)
        {
            if ( $timer->stop != null )
            {
                $timerTotal += $timer->stop - $timer->start;
            }
        }
        echo $name, ': ', $timerTotal, $nl;

    }
}   
?>

Upvotes: 3

Rok Kralj
Rok Kralj

Reputation: 48725

If you want to do it in a OO manner, you can have a class, where you start.

$timer->start('piece1');
//code1
$timer->stop('piece1');
echo 'Script piece1 took '.$timer->get('piece1').' ms';

I believe it is done like that in codeigniter framework.

The point of these names ('piece1') is that you can have multiple timers running at the same time (example one in another). The code for implementihg is fairly simple, about 10 lines.

Upvotes: 1

Related Questions