note 100633 deleted from function.microtime by danbrown
| From: | danbrown@php.net | Date: | Wed, 27 Oct 2010 20:15:17 +0000 |
| Subject: | note 100633 deleted from function.microtime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173523@lists.php.net to get a copy of this message | ||
Note Submitter: dsysko at genscape dot com
----
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