PHP Classes

PHP StopWatch Class: Measures script execution time between steps

Recommend this page to a friend!
  Info   Example   Screenshots   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: 254 All time: 7,870 This week: 660Up
Version License PHP version Categories
stop-watch 1.0.0GNU General Publi...5.0PHP 5, Time and Date
Description 

Author

This class can measures script execution time between steps.

It can start measuring the time when a class object is created.

The class can take note of partial time for given steps and assign them a text label.

In the end it can return an array with the times it took to reach each step, as well the time between steps.

Picture of Gianluca Zanferrari
  Performance   Level  
Name: Gianluca Zanferrari <contact>
Classes: 19 packages by
Country: The Netherlands The Netherlands
Age: 58
All time rank: 1674 in The Netherlands The Netherlands
Week rank: 50 Up1 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php
require_once('stopwatch.class.php');

// initiate the class and set the start flag
$sw = new stopWatch;

// let the script sleep for half a second
usleep(500000);

// set a benchmark flag named 'step one'
$sw->setFlag('step one');

usleep(1500000);

// set a benchmark flag named 'step two'
$sw->setFlag('step two');

// let the script sleep for 3/4 seconds
usleep(750000);

// set a benchmark flag named 'step three'
$sw->setFlag('step three');

// let the script sleep for 2 seconds
usleep(2000000);

// set the stop flag
$sw->stop();

// dump setted flags during the execution of the script
print_r('<pre>');
var_dump($sw->time);
print_r('</pre>');

// print out the results programmatically
echo('<h3>Execution time TILL a given step</h3>');
echo(
'Execution time (in seconds) till step one: ' . $sw->till_step['step one'] . '<br>');
echo(
'Execution time (in seconds) till step two: ' . $sw->till_step['step two'] . '<br>');
echo(
'Execution time (in seconds) till step three: ' . $sw->till_step['step three'] . '<br>');
echo(
'Total execution time of the script: ' . $sw->time['execution_time']);

echo(
'<h3>Execution time OF a given step</h3>');
echo(
'Execution time (in seconds) of step one: ' . $sw->duration_step['step one'] . '<br>');
echo(
'Execution time (in seconds) of step two: ' . $sw->duration_step['step two'] . '<br>');
echo(
'Execution time (in seconds) of step three: ' . $sw->duration_step['step three'] . '<br>');
echo(
'Execution time (in seconds) from last step to the end: ' . $sw->duration_step['end'] . '<br>');


Screenshots (1)  
  • index.png
  Files folder image Files (3)  
File Role Description
Plain text file stopwatch.class.php Class class itself
Accessible without login Plain text file stopwatch.example.php Example example

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:254
This week:0
All time:7,870
This week:660Up
User Comments (1)
nice work!
10 years ago (J Diamond)
70%StarStarStarStar