Skip to content

Instantly share code, notes, and snippets.

@nyson
Created March 27, 2012 09:41
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 nyson/2214421 to your computer and use it in GitHub Desktop.
Save nyson/2214421 to your computer and use it in GitHub Desktop.
Short snippet to evaluate execution time from a function in php
<?
/**
* Gets execution time of a closure (Anonymous Function)
*
* @param $action Closure to execute
* @param $return [optional] Pass reference to recieve return values
*
* @return execution time of the closure in seconds in float
*/
function getExecutionTime(Closure $action, &$return=null){
$mtime = microtime(true);
$return = $action();
return microtime(true) - $mtime;
}
/***~~~... EXAMPLE ...~~~***/
$test = function (){
// test code here
};
echo "Code executed in " . getExecutionTime($test) . " ms!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment