I wanted to find out whether echo would be quicker in small chunks or one large chunk. The following experiment shows that there is no significant difference between the two. I ran two test cases, one with a string that is 1000000 bytes long and another with a string length of 1000000. Here are the results. As you can see I ran 5 test cases. The column on the left is the time it took to echo one big chunk. the column on the left is how long it took to echo several smaller chunks. The source code follows below..
10^5
1.49199795723 1.54602003098
1.52049994469 1.53507804871
1.5225379467 1.58841896057
1.31378698349 1.52398896217
1.57401800156 1.51450896263
10^6
15.09724617 14.4749720097
14.5426721573 15.6235468388
15.2025880814 13.6396179199
15.7211170197 15.715692997
15.2420709133 14.0160560608
function echobig($string, $bufferSize = 8192)
{
$splitString = str_split($string, $bufferSize);
foreach($splitString as $chunk)
echo $chunk;
}
global $dat;
$dat = "";
function testit()
{
global $dat;
$data = "";
for( $a = 0; $a <= 1000000; $a += 1 ) $data .= "a";
$u1= microtime(true);
echobig( $data );
$u2= microtime(true);
echo $data;
$u3= microtime(true);
$diff = $u2 - $u1;
$diff2= $u3 - $u2;
$dat .= "$diff2 $diff $u2 $u1\r\n";
}
$i = 0;
while( $i < 5 )
{
testit(); $i += 1;
}
global $dat;
$fp = fopen( "../Data/results.txt", "w" );
fwrite( $fp, $dat );
fclose( $fp );
Programming Language Questions