note 100633 added to function.microtime
| From: | dsysko at genscape dot com | Date: | Wed, 27 Oct 2010 18:32:26 +0000 |
| Subject: | note 100633 added to function.microtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173513@lists.php.net to get a copy of this message | ||
Paul,
Using PHP5, your example will produce invalid results when the run time >= 1 second.
The reason is that the format of the output of microtime() is:
microseconds . ' ' . seconds (since epoch)
You are only working with the substring before ' '.
This behaviour is for backwards compatibility with PHP4.
Substitute "microtime(TRUE)" for every instance of "microtime()" in your second
code and it will yield more accurate results, although the precision will be incorrect.
Given the standard PHP5 installation, this code will show you what I mean, as well as providing a
string with the precision matched to the output of microtime():
<?php
$strFileContent = file_get_contents('/path/to/large/text/file');
$arrTemp = array();
$i = 0;
$key = '';
$value = '';
$tsStartOne = microtime();
for($i=10;$i>0;$i--){
$arrTemp = explode("\n", $strFileContent);
foreach($arrTemp as $key=>$value) {
if(trim($value) == '') {
unset($arrTemp[$key]);
}
}
sort($arrTemp);
}
$tsEndOne = microtime();
unset($arrTemp,$i,$key,$value);
$arrTemp = array();
$i = 0;
$key = '';
$value = '';
$tsStartTwo = microtime(TRUE);
for($i=10;$i>0;$i--){
$arrTemp = explode("\n", $strFileContent);
foreach($arrTemp as $key=>$value) {
if(trim($value) == '') {
unset($arrTemp[$key]);
}
}
sort($arrTemp);
}
$tsEndTwo = microtime(TRUE);
echo "microtime():\t\tstart=$tsStartOne\tend=$tsEndOne\n";
echo "microtime(TRUE):\tstart=$tsStartTwo\t\tend=$tsEndTwo\n";
$intParseTimeOne = $tsEndOne-$tsStartOne;
$strParseTimeChk = elapsedPreciseTimeOne($tsEndOne,$tsStartOne);
$intParseTimeTwo = elapsedPreciseTimeTwo($tsEndTwo,$tsStartTwo);
echo "Parse Time Paul:\t".$intParseTimeOne." seconds\n";
echo "Parse Time Fix: \t".$strParseTimeChk." seconds\n";
echo "Parse Time TRUE:\t".$intParseTimeTwo." seconds\n";
function elapsedPreciseTimeOne($tsEnd , $tsStart) {
list($intEndUSec,$intEndSec) = explode(' ', $tsEnd);
list($intStartUSec,$intStartSec) = explode(' ', $tsStart);
$intElapsedUSec = $intEndUSec - $intStartUSec;
$intElapsedSec = $intEndSec - $intStartSec;
if($intElapsedUSec < 0) {
$intElapsedUSec = (1 + $intElapsedUSec);
$intElapsedSec --;
}
$intDecimalPrecision = min(strlen($intEndUSec) , strlen($intStartUSec)) - 1;
$strElapsedUSec = str_pad(strstr($intElapsedUSec, '.') , $intDecimalPrecision ,
'0' , STR_PAD_RIGHT);
$strPreciseTime = $intElapsedSec . $strElapsedUSec;
return $strPreciseTime;
}
function elapsedPreciseTimeTwo($tsEnd , $tsStart) {
$intDecimalPrecision = strlen(substr($tsEnd , (strpos($tsEnd, '.') + 1)));
$intPreciseTime = round(($tsEnd - $tsStart) , $intDecimalPrecision);
return $intPreciseTime;
}
?>
Output should be similar to:
microtime(): start=0.54014400 1288203513 end=0.23241700 1288203516
microtime(TRUE): start=1288203516.2534 end=1288203518.9984
Parse Time Paul: -0.307727 seconds
Parse Time Fix: 2.69227300 seconds
Parse Time TRUE: 2.745 seconds
----
Server IP: 208.69.120.55
Probable Submitter: 67.50.94.82
----
Manual Page -- http://www.php.net/manual/en/function.microtime.php
Edit -- https://master.php.net/note/edit/100633
Del: integrated -- https://master.php.net/note/delete/100633/integrated
Del: useless -- https://master.php.net/note/delete/100633/useless
Del: bad code -- https://master.php.net/note/delete/100633/bad+code
Del: spam -- https://master.php.net/note/delete/100633/spam
Del: non-english -- https://master.php.net/note/delete/100633/non-english
Del: in docs -- https://master.php.net/note/delete/100633/in+docs
Del: other reasons-- https://master.php.net/note/delete/100633
Reject -- https://master.php.net/note/reject/100633
Search -- https://master.php.net/manage/user-notes.php