PHP Classes

Central Timer: Measure the time elapsed between code sections

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 106 All time: 9,710 This week: 455Up
Version License PHP version Categories
central-timer 1GNU General Publi...5.3PHP 5, Time and Date
Description 

Author

This class can measure the time elapsed between code sections.

It can record the current time and return the time that elapsed after a certain section code was executed.

The class can return multiple timer object instances to be able to measure the time that elapsed when running different code sections.

Picture of Jacob Fogg
  Performance   Level  
Name: Jacob Fogg <contact>
Classes: 7 packages by
Country: United States United States
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Example

<?php
require('Timer.php');

use
Fogg\Util\Timer\Timer;

//Instantiate a timer object directly
echo "Start the direct timer.<br>\n";
$directTimer = new Timer();

//Instantiate a timer object via the static method
echo "Start the 'central' timer.<br>\n";
$centralTimer = Timer::get_instance('central');

//Pause the script for 2 seconds
echo "Pause for 2 seconds.<br>\n";
sleep(2);

//get the direct timer duration
echo "Direct timer duration (2 seconds): {$directTimer->elapsed()}<br>\n";

//reset the direct timer
echo "Resetting the direct timer.<br>\n";
$directTimer->reset();

//Instantiate a second timer object via the static method
//Not going to store the timer, because we can access it later through the same static method
echo "Start the 'secondTimer', but don't store the instance.<br>\n";
Timer::get_instance('secondTimer');

//Pause the script for 2 more seconds
echo "Pause for 2 more seconds.<br>\n";
sleep(2);

//display the durations of the three timers
echo "Direct timer duration (2 seconds): {$directTimer->elapsed()}<br>\n";
echo
"'central' timer duration (4 seconds): {$centralTimer->elapsed()}<br>\n";
echo
"Fetch the 'secondTimer' instance.<br>\n";
$secondTimer = Timer::get_instance('secondTimer');
echo
"'secondTimer' duration (2 seconds): {$secondTimer->elapsed()}<br>\n";


  Files folder image Files (2)  
File Role Description
Files folder imageFogg (1 directory)

  Files folder image Files (2)  /  Fogg  
File Role Description
Files folder imageUtil (1 directory)

  Files folder image Files (2)  /  Fogg  /  Util  
File Role Description
Files folder imageTimer (2 files)

  Files folder image Files (2)  /  Fogg  /  Util  /  Timer  
File Role Description
  Accessible without login Plain text file Example.php Example Example file
  Plain text file Timer.php Class Timer class file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:106
This week:0
All time:9,710
This week:455Up