<?php

if (isset($_SERVER['SERVER_PROTOCOL'])) { echo "<html><head><title>microtime conversion timing comparison</title></head><body><pre>\n"; }

$iterations=1000000;
$sortResults=true;

$usedTime=array();
$output=array();

$time=microtime();

echo 
"microtime() result being worked on: '".$time."'\n\n";
echo 
"Iterations for each method: ".number_format($iterations)."\n\n";

echo 
"Running test:";

function 
microtime_used($before,$after) {
    return (
substr($after,11)-substr($before,11))
        +(
substr($after,0,9)-substr($before,0,9));
    
/*
    $sec=substr($after,11)-substr($before,11);
    $usec=substr($after,0,9)-substr($before,0,9);
    return $sec+$usec;
    // */
}

$testsbegin=microtime();




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" strtok+add";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$test=strtok($time,' ')+strtok(' ');
}
$after=microtime();

$usedTime['strtok+add']=microtime_used($before,$after);
$output['strtok+add']=$test;




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" explode+list+float";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    list(
$usec,$sec)=explode(' ',$time);
    
$test=(float)$usec+(float)$sec;
}
$after=microtime();

$usedTime['explode+list+float']=microtime_used($before,$after);
$output['explode+list+float']=$test;

if (isset(
$usec)) { unset($usec); }
if (isset(
$sec)) { unset($sec); }




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" explode+float";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$arr=explode(' ',$time);
    
$test=(float)$arr[0]+(float)$arr[1];
}
$after=microtime();

$usedTime['explode+float']=microtime_used($before,$after);
$output['explode+float']=$test;

if (isset(
$arr)) { unset($arr); }




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" explode+list+add";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    list(
$usec,$sec)=explode(' ',$time);
    
$test=$usec+$sec;
}
$after=microtime();

$usedTime['explode+list+add']=microtime_used($before,$after);
$output['explode+list+add']=$test;

if (isset(
$usec)) { unset($usec); }
if (isset(
$sec)) { unset($sec); }




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" explode+add";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$arr=explode(' ',$time);
    
$test=$arr[0]+$arr[1];
}
$after=microtime();

$usedTime['explode+add']=microtime_used($before,$after);
$output['explode+add']=$test;

if (isset(
$arr)) { unset($arr); }




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" explode+array_sum";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$test=array_sum(explode(' ',$time));
}
$after=microtime();

$usedTime['explode+array_sum']=microtime_used($before,$after);
$output['explode+array_sum']=$test;



if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

if (
function_exists('bcadd')) {
    echo 
" explode+bcadd";
    
    
$before=microtime();
    for(
$i=0;$i<$iterations;$i++) {
        
$temp=explode(' ',$time);
        
$test=bcadd($temp[0],$temp[1],6);
    }
    
$after=microtime();
    
    
$usedTime['explode+bcadd']=microtime_used($before,$after);
    
$output['explode+bcadd']=$test;
    
    if (isset(
$temp)) { unset($temp); }
}



if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" preg_replace";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$test=preg_replace('/^0?(\S+) (\S+)$/X','$2$1',$time);
}
$after=microtime();

$usedTime['preg_replace']=microtime_used($before,$after);
$output['preg_replace']=$test;




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" substr+dot";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$test=substr($time,11,10).substr($time,1,7);
}
$after=microtime();

$usedTime['substr+dot']=microtime_used($before,$after);
$output['substr+dot']=$test;




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" substr+add";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$test=substr($time,0,9)+substr($time,-10);
}
$after=microtime();

$usedTime['substr+add']=microtime_used($before,$after);
$output['substr+add']=$test;




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" substr+dot-2";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$test=substr($time,11).substr($time,1,9);
}
$after=microtime();

$usedTime['substr+dot-2']=microtime_used($before,$after);
$output['substr+dot-2']=$test;




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" substr+add-2";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$test=substr($time,11)+substr($time,1,9);
}
$after=microtime();

$usedTime['substr+add-2']=microtime_used($before,$after);
$output['substr+add-2']=$test;




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" explode+substr+dot";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$arr=explode(' ',$time);
    
$test=$arr[1].substr($arr[0],1);
}
$after=microtime();

$usedTime['explode+substr+dot']=microtime_used($before,$after);
$output['explode+substr+dot']=$test;

if (isset(
$arr)) { unset($arr); }




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }

echo 
" strpos+substr+dot";

$before=microtime();
for(
$i=0;$i<$iterations;$i++) {
    
$pos=strpos($time,' ');
    
$test=substr($time,$pos+1).substr($time,1,$pos-1);
}
$after=microtime();

$usedTime['strpos+substr+dot']=microtime_used($before,$after);
$output['strpos+substr+dot']=$test;




if (isset(
$before)) { unset($before); }
if (isset(
$after)) { unset($after); }
if (isset(
$test)) { unset($test); }
if (isset(
$i)) { unset($i); }


$testsend=microtime();
$teststime=microtime_used($testsbegin,$testsend);

echo 
"\n\n";

if (
$sortResults) {
    
asort($usedTime);
}

if (
count($usedTime)<1) {
    echo 
"No methods tried.\n";
} else {
    
$keylen=0;
    
$timelen=0;
    
$looptimelen=0;
    
$outputlen=0;
    
$timeSum=0;
    foreach(
$usedTime as $key=>$time) {
        
$keylen=max($keylen,strlen($key));
        
$timelen=max($timelen,strlen($time));
        
$looptimelen=max($looptimelen,strlen($time/$iterations));
        
$outputlen=max($outputlen,strlen($output[$key]));
        
$timeSum+=$time;
    }
    
$timeAvg=$timeSum/count($usedTime);
    
$keylen=max($keylen,strlen("total"));
    
$timelen=max($timelen,strlen($timeSum));
    
$looptimelen=max($looptimelen,strlen($timeSum/$iterations));
    
$keylen=max($keylen,strlen("average"));
    
$timelen=max($timelen,strlen($timeAvg));
    
$looptimelen=max($looptimelen,strlen($timeAvg/$iterations));
    
    
    
    foreach(
$usedTime as $key=>$time) {
        echo 
str_pad($key,$keylen)." : total ".str_pad($time,$timelen)
            .
" sec, per loop ".str_pad($time/$iterations,$looptimelen)
            .
" sec, output: ".str_pad("'".$output[$key]."'",$outputlen+2)."\n";
    }
    echo 
str_pad("total",$keylen)." : total ".str_pad($timeSum,$timelen)
        .
" sec, per loop ".str_pad($timeSum/$iterations,$looptimelen)
        .
" sec\n";
    echo 
str_pad("average",$keylen)." : total ".str_pad($timeAvg,$timelen)
        .
" sec, per loop ".str_pad($timeAvg/$iterations,$looptimelen)
        .
" sec\n";
}

echo 
"\nTests took ".$teststime." sec total to perform.\n";

if (isset(
$_SERVER['SERVER_PROTOCOL'])) { echo "\n</pre></body></html>\n"; }

?>