PHP code example of razra / stopwatch

1. Go to this page and download the library: Download razra/stopwatch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

razra / stopwatch example snippets


    $stopwatch = new Stopwatch('someName');
    

    $stopwatch = Stopwatch::start('someName');
    // or without parameter (that stopwatch will try to get name of method where its called)
    $stopwatch = Stopwatch::start();
    

    $stopwatch->stop();
    

    // it will create html page with report of stopwatches in given path
    $reporter = new HtmlReported('path/to/your/folder');
    $reporter->report();
    




zra\Component\Stopwatch\HtmlReported;
use Razra\Component\Stopwatch\Stopwatch;

// start our first stopwatch
$stopwatch = Stopwatch::start('Application');

class StopwatchExample
{
    public function first()
    {
        // start our second stopwatch
        $stopwatch = Stopwatch::start();

        for ($i =0; $i < 100000000; $i++) {
        }

        // stop second stopwatch and reset
        $stopwatch->stop()->reset();

        // do something...
        
        // stop second stopwatch and reset
        // it will have time difference between prev reset and this stop
        $stopwatch->stop()->reset();

        // do something
        
        // stop second stopwatch
        $stopwatch->stop();
    }

    public function second()
    {
        // start our third stopwatch
        $stopwatch = Stopwatch::start();

        // stop third stopwatch
        $stopwatch->stop();
    }

    public function third()
    {
        // start our fourth stopwatch
        $stopwatch = Stopwatch::start();

        // stop fourth stopwatch
        $stopwatch->stop();
    }
}

// call methods
$stopwatchExample = new StopwatchExample();
$stopwatchExample->first();
$stopwatchExample->second();
$stopwatchExample->third();

$pathForReports = __DIR__ . '/reported';

if (! is_dir($pathForReports)) {
    mkdir($pathForReports);
}

// stop our first stopwatch
$stopwatch->stop();

// report our stopwatches, will put html page in $pathForReports path
$reporter = new HtmlReported($pathForReports);
$reporter->report();