Last active
October 23, 2015 14:32
-
-
Save joshdutcher/de65b8f3fe6a2c8efd42 to your computer and use it in GitHub Desktop.
time_elapsed() - function to track php script execution time. To find where the code is slow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function time_elapsed() { | |
$backtrace = debug_backtrace(); | |
$calling_file = $backtrace[0]['file']; | |
$calling_line = $backtrace[0]['line']; | |
$call_string = $calling_file . ' line ' . $calling_line . '<br/>'; | |
static $last = null; | |
$now = microtime(true); | |
if ($last == null) { | |
$outstring = $call_string . 'Starting timer...<br/>'; | |
} else { | |
$elapsed = ($now - $last); | |
$outstring = $call_string . 'Elapsed: ' . round($elapsed, 4) . ' seconds'; | |
} | |
echo $outstring; | |
$last = $now; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use once to start the timer. Each use thereafter within the same script will output how long it took to get to it from the previous use. The
$last
variable resets tonull
at each new script execution.Example use:
will output
Looks nicer if you replace
echo
withdebug
from my other gist, debug.