In many cases, it is really useful to know the exact time for how long a PHP script is running or how long will take PHP web page load. On the other hand it is also useful to know that how long a particular script operation or part of page load will take.
Following PHP Timing class is very simple to use, only create class and start timing and stop timing. Elapsed time function can be used also (elapsed time is calculated from script start time). Statistics can be displayed in print-functions and the same information without any formatting can also get with get-functions.
PHP Timing class
<?php class Timing { private $break; private $start_time; private $stop_time; // Constructor for Timing class public function __construct($break = "<br />") { $this->break = $break; // Set timezone date_default_timezone_set('UTC'); } // Set start time public function start() { $this->start_time = microtime(true); } // Set stop/end time public function stop() { $this->stop_time = microtime(true); } // Returns time elapsed from start public function getElapsedTime() { return $this->getExecutionTime(microtime(true)); } // Returns total execution time public function getTotalExecutionTime() { if (!$this->stop_time) { return false; } return $this->getExecutionTime($this->stop_time); } // Returns start time, stop time and total execution time public function getFullStats() { if (!$this->stop_time) { return false; } $stats = array(); $stats['start_time'] = $this->getDateTime($this->start_time); $stats['stop_time'] = $this->getDateTime($this->stop_time); $stats['total_execution_time'] = $this->getExecutionTime($this->stop_time); return $stats; } // Prints time elapsed from start public function printElapsedTime() { echo $this->break . $this->break; echo "Elapsed time: " . $this->getExecutionTime(microtime(true)); echo $this->break . $this->break; } // Prints total execution time public function printTotalExecutionTime() { if (!$this->stop_time) { return false; } echo $this->break . $this->break; echo "Total execution time: " . $this->getExecutionTime($this->stop_time); echo $this->break . $this->break; } // Prints start time, stop time and total execution time public function printFullStats() { if (!$this->stop_time) { return false; } echo $this->break . $this->break; echo "Script start date and time: " . $this->getDateTime($this->start_time); echo $this->break; echo "Script stop end date and time: " . $this->getDateTime($this->stop_time); echo $this->break . $this->break; echo "Total execution time: " . $this->getExecutionTime($this->stop_time); echo $this->break . $this->break; } // Format time to date and time private function getDateTime($time) { return date("Y-m-d H:i:s", $time); } // Get execution time by timestamp private function getExecutionTime($time) { return $time - $this->start_time; } } ?>
PHP Timing class usage example 1
<?php // Create new Timing class with \n break $timing = new Timing("\n"); // Start timing $timing->start(); // Loop ten rounds and sleep one second per round for ($i=1;$i<=10;$i++) { echo $i . "\t"; sleep(1); // Print elapsed time every 2 rounds if ($i%2==0) { $timing->printElapsedTime(); } } // Stop/end timing $timing->stop(); // Print only total execution time $timing->printTotalExecutionTime(); // Print full stats $timing->printFullStats(); ?>
Example output:
$ php timing_example1.php 1 2 Elapsed time: 2.0003080368042 3 4 Elapsed time: 4.0006020069122 5 6 Elapsed time: 6.0008690357208 7 8 Elapsed time: 8.0011501312256 9 10 Elapsed time: 10.001409053802 Total execution time: 10.001439094543 Script start date and time: 2010-01-08 13:19:17 Script stop end date and time: 2010-01-08 13:19:27 Total execution time: 10.001439094543 $
PHP Timing class usage example 2
<?php // Create new Timing class with \n break $timing = new Timing("\n"); // Start timing $timing->start(); $elapsed_times = array(); // Loop ten rounds and sleep one second per round for ($i=1;$i<=10;$i++) { echo $i . "\t"; sleep(1); // Get elapsed time every rounds $elapsed_times[$i] = $timing->getElapsedTime(); } echo "\n\n"; // Stop/end timing $timing->stop(); // Get full stats $times = $timing->getFullStats(); foreach ($elapsed_times as $key => $t) { $t2; if (isset($elapsed_times[$key-1])) { $t2 = $t - $elapsed_times[$key-1]; } else { $t2 = $t; } echo "Round: " . $key . " Time: " . $t2 . "\n"; } echo "Total execution time: " . $times['total_execution_time'] . "\n"; echo $times['start_time'] . " - " . $times['stop_time'] . "\n"; ?>
Example output:
$ php timing_example2.php 1 2 3 4 5 6 7 8 9 10 Round: 1 Time: 1.0000989437103 Round: 2 Time: 1.0001330375671 Round: 3 Time: 1.000118970871 Round: 4 Time: 1.0001170635223 Round: 5 Time: 1.0001168251038 Round: 6 Time: 1.0001120567322 Round: 7 Time: 1.000118970871 Round: 8 Time: 1.0001220703125 Round: 9 Time: 1.0001239776611 Round: 10 Time: 1.0001330375671 Total execution time: 10.001236915588 2010-01-08 13:19:47 - 2010-01-08 13:19:57 $
Related posts:
- PHP Class for Coloring PHP Command Line (CLI) Scripts Output – PHP Output Colorizing Using Bash Shell Colors PHP Command Line Interface (CLI) has not built-in coloring for script output, like example Perl language has (perldoc.perl.org/Term/ANSIColor.html). So I...
- BitLy (bit.ly) PHP Class – Shorten and Expand URLs (and Hashes) with BitLy API BitLy (bit.ly) is a service which allows users to shorten, expand, share and track URLs (links). bit.ly can be accessed...
- PHP: Loop through dates (from date to date) with strtotime() function This is very easy way loop through dates (from date to date) with PHP strtotime() function. This example only echo...
- CSS compression with own PHP class VS CSSTidy It makes sense to optimize the site's cascading style sheets (CSS), as it pages load faster, and it reduce the...
- PHP __DIR__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__, __NAMESPACE__ PHP has large number of predefined constants. This HOWTO will present the seven most important, most practical and most useful...
Very helpful! Thanks for sharing JR.