PHP Classes

File: stopwatch.example.php

Recommend this page to a friend!
  Classes of Gianluca Zanferrari   PHP StopWatch Class   stopwatch.example.php   Download  
File: stopwatch.example.php
Role: Example script
Content type: text/plain
Description: example
Class: PHP StopWatch Class
Measures script execution time between steps
Author: By
Last change:
Date: 10 years ago
Size: 1,584 bytes
 

Contents

Class file image Download
<?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>');