Hi All, if you are wondering where your processes are taking time, this will be of use to you.
The function gmicrotime() below stores time stamps to the millisecond in an array called $mT['all'][] each time it is called. However, if you pass a parameter for example gmicrotime('subprocess'), it also stores the same timestamp in another array $mT['subprocess'][] as well.
When you call gmicrotime the first time for any given parameter, it will return nothing. However, the second time it will return the time elapsed since the previous call - for that parameter. This is a great way to use the function to track processes and even sub-processes, using only one function. Paste the following onto a page and watch it work:
function gmicrotime($n=''){
#version 1.1, 2007-05-09
//store array of all calls in mT
global $mT;
list($usec, $sec) = explode(' ',microtime());
$t=round((float)$usec + (float)$sec,6);
$mT['all'][]=$t;
if($n)$mT[$n][]=$t;
//if more than one timestamp for the given key, return elapsed time since previous timestamp
$u=($n?$mT[$n]:$mT['all']);
if(count($u)>1)return round(1000*($u[count($u)-1]-$u[count($u)-2]),6);
}
function dosomething(){
//just some arbitrary function that takes time to process
global $qr, $qx;
gmicrotime('function_runtime');
//sub-process
gmicrotime('sub1');
$qr['idx1']=$r=rand(1,10);
for($i=1;$i<=100000*$r;$i++){
$x=$x+1;
}
$qr['sub1']=gmicrotime('sub1');
//sub-process
gmicrotime('sub2');
$qr['idx2']=$r=rand(1,10);
for($i=1;$i<=100000*$r;$i++){
$x=$x+1;
}
$qr['sub2']=gmicrotime('sub2');
$qr['function_runtime']=gmicrotime('function_runtime');
$qx[]=$qr;
}
//---------------- begin the page ------------------------
echo 'page starting at time '.time().'<br>';
//first call to microtime with key "process"
gmicrotime('page_runtime');
//run the page process
for($i=1;$i<=5;$i++){
echo 'running dosomething()..<br>';
dosomething();
}
echo 'page ending at time '.time().'<br>';
//second call to microtime with key "process" - so it returns the time delta
echo 'microtime: '.gmicrotime('page_runtime');
echo '<pre>';
//list the timestamp reports for each time the function was called
print_r($qx);
//list all the microtime timestamps we created
print_r($mT);
Sincerely,
Samuel Fullman