Created
September 23, 2009 13:40
-
-
Save evaisse/192010 to your computer and use it in GitHub Desktop.
Bench function, just bench('flagname '.__LINE__) to set a flag, and bench() to get bench datas.
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
<?php #-*- coding:utf-8 -*- | |
# function bench() | |
/** | |
* | |
* @param string $label flag to markup your bench step | |
* @return mixed | |
* @example | |
bench('big ass code N#1 @ '.__LINE__); | |
// some code.. | |
bench('Another stupid test @ '.__LINE__); | |
// some code.. | |
print_r(bench()); // => results | |
* | |
*/ | |
function bench($label='nolabel') | |
{ | |
static $stack=null; | |
static $start=null; | |
if(is_null($stack)) $stack = array(); | |
list($u, $s) = explode(" ", microtime()); | |
$utime = ((float)$u + (float)$s); | |
if(isset($stack[0])) $delta = $utime - $stack[count($stack)-1]['utime']; | |
else $delta = 0; | |
$total = (!isset($stack[0])) ? 0 | |
: ($utime - $stack[0]['utime']); | |
$stack[] = array( 'flag' => (string)$label, | |
'utime' => $utime, | |
'delta' => '+'.round($delta,5).'s', | |
'total' => round($total,5).'s', | |
'memory' => function_exists('memory_get_usage') ? memory_get_usage() : 0 ); | |
return $stack; | |
} | |
//- bench() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment