PHP Classes

PHP StopWatch Class: Measures script execution time between steps

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 254 This week: 1All time: 7,866 This week: 560Up
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: 57
All time rank: 1674 in The Netherlands The Netherlands
Week rank: 109 Up2 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  
  • index.png
  Files folder image Files  
File Role Description
Plain text file stopwatch.class.php Class class itself
Accessible without login Plain text file stopwatch.example.php Example example

 Version Control Unique User Downloads Download Rankings  
 0%
Total:254
This week:1
All time:7,866
This week:560Up
User Comments (1)
nice work!
9 years ago (J Diamond)
70%StarStarStarStar