Skip to content

Instantly share code, notes, and snippets.

@malja
Created September 6, 2018 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malja/78ac4523a588b65d052e9d3ae99e2c17 to your computer and use it in GitHub Desktop.
Save malja/78ac4523a588b65d052e9d3ae99e2c17 to your computer and use it in GitHub Desktop.
PHP class for measuring function execution time
<?php
/***********************************************************************\
|
| RunTime Class
| =============
| Class to measuring the execution time of function or method.
|
| Author: malja <github.com/malja>
| Version: 1.0
| Created: 26. 8. 2013
|
| Licence: CC-BY ( http://creativecommons.org/licenses/by/3.0/ )
|
| ---------------------------------------------------------------------
|
| Example
| =======
|
| Note: Considering you have a class with name "MyClassName" with public
| method "methodName". Method must take three arguments (int, int,
| string).
|
| $time = new RunTime();
| $time->runMethod("MyClassName", "methodName", array(5,4, "Hi"), 500)
| echo $time->getString();
|
\***********************************************************************/
class RunTime {
private $time_iterations;
private $time_iteration_start;
private $time_overall;
private $arguments_string;
private $round_at;
/**
* Constructor
**/
public function __construct() {
$this->time_iterations = array();
}
/**
* Call simple function $iterations times and save the overall time
*
* string $function - Name of function which should be tested
* array $arguments - Array of arguments for function
* int $iterations - Number of loops
**/
public function runFunction($function, $arguments, $iterations = 1) {
// If function with given name does not exists
if (!function_exists($function)) {
// Create new Exception with error message
throw new Exception("Function " . $function . " does not exists.");
}
// If loops count is lower than/equals zero
if ($iterations <= 0) {
// Stop working ...
return;
}
// If variable $arguments is not of type array
if (!is_array($arguments)) {
// Make it an array
$arguments = array($arguments);
}
// Loop
for ($it = 0; $it != $iterations; $it++) {
// Get start time
$this->time_iteration_start = microtime(true);
// Call given function
call_user_func_array($function, $arguments);
// Get finish time (current_time - startime) and save it to array for later
array_push($this->time_iterations, microtime(true) - $this->time_iteration_start);
}
// Count the overall time of all calls
$this->time_overall = array_sum($this->time_iterations);
}
/**
* Call method (member function) of class $iterations times and save the overall time
*
* string $class - Class name
* string $method - Method name of given class_alias
* array $arguments - Array of arguments for function_exists
* int $iterations - Number of loops
**/
public function runMethod($class, $method, $arguments, $iterations = 1) {
// If class with given name does not exists
if (!class_exists($class)) {
// Create an new Exception with error message
throw new Exception("Class " . $class . " does not exists.");
}
// Create new instance of class
$classInstance = new $class();
// If our class does not have method with given name
if (!method_exists($class, $method)) {
// Again throw an Exception with error message
throw new Exception("Class " . $class . " does not have method " . $method . ".");
}
// If the number of loops is lower than/equals zero
if ($iterations <= 0) {
// Do nothing ...
return;
}
// If $arguments parameter is not of type array
if (!is_array($arguments)) {
// Make it an array
$arguments = array($arguments);
}
// Main loop
for ($it = 0; $it != $iterations; $it++) {
// Get start time
$this->time_iteration_start = microtime(true);
// Call given method
call_user_func_array(array($classInstance, $method), $arguments);
// Save finish time (current_time - start_time) and push it to array
array_push($this->time_iterations, microtime(true) - $this->time_iteration_start);
}
// Count the overall time for later
$this->time_overall = array_sum($this->time_iterations);
}
/**
* int [$round] - Number of decimal points (default = 6)
**/
public function getString($round = 6) {
// Sort the array of iterations time from the highest to the lowest
$this->time_time_iterations = arsort($this->time_iterations);
// What if the $round parameter is not of type int?
if (!is_numeric($round) || $round < 0) {
// Set it to default value
$round = 6;
}
// Print output
return "Iterations: " . count($this->time_iterations) . "<br/>" .
"Total time: " . number_format($this->time_overall, $round) . "s<br/>" .
"Max time: " . number_format($this->time_iterations[0], $round) . "s<br/>" .
"Average time: " . number_format($this->time_overall / count($this->time_iterations), $round) . "s<br/>" .
"Min time: " . number_format($this->time_iterations[count($this->time_iterations)-1] , $round) . "s<br/>";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment