Translations of this page?:

Performance test STRING against ARRAY

The following script gives leads to this results:

Execution time for concat = 2.37720680237 seconds. Output in 0.596145153046 seconds
Execution time for array = 0.0890350341797 seconds. Output in 0.82287311554 seconds

This shows up that using an array could be up to 3 times faster than using string-concatenation. The impact to existing plugins is quite small. Just replacing the ”.=” by ”[]=”.

I can not reproduce your findings using PHP 4.3.10 using the slightly modified code below. I added output buffering which produces slightly better results but not much. My results:
Execution time for array  = 0.04479 seconds. Output in 0.135224 seconds
Execution time for concat = 0.049488 seconds. Output in 0.09766 seconds
Execution time for output buffering = 0.034316 seconds. Output in 0.095657 seconds
<?php
 
function test_concatenation($times, $str="The Quick brown fox jumps over the lazy dog.<br>\n") {
  for($i=0 ; $i<$times ; $i++) {
    $string .= $str;
  }
 
  return $string;
}
 
 
function test_array($times, $str="The Quick brown fox jumps over the lazy dog.<br>\n") {
  for($i=0 ; $i<$times ; $i++) {
    $string_arr[] = $str;
  }
 
  return $string_arr;
}
 
function test_ob($times, $str="The Quick brown fox jumps over the lazy dog.<br>\n") {
  ob_start();
  for($i=0 ; $i<$times ; $i++) {
    echo $str;
  }
  $string = ob_get_contents();
  ob_end_clean();
  return $string;
}
 
 
$times = 30000;
$result = array();
 
$ta0 = microtime(true);
$string_arr = test_array($times);
$ta1 = microtime(true);
foreach($string_arr as $string) echo $string;
$ta2 = microtime(true);
$ta_a=$ta1-$ta0;
$ta_b=$ta2-$ta1;
$result[] = "Execution time for array  = $ta_a seconds. Output in $ta_b seconds<br>\n";
 
$tc0 = microtime(true);
$string = test_concatenation($times);
$tc1= microtime(true);
echo $string;
$tc2= microtime(true);
$tc_a=$tc1-$tc0;
$tc_b=$tc2-$tc1;
$result[] = "Execution time for concat = $tc_a seconds. Output in $tc_b seconds<br>\n";
 
$tc0 = microtime(true);
$string = test_ob($times);
$tc1= microtime(true);
echo $string;
$tc2= microtime(true);
$tc_a=$tc1-$tc0;
$tc_b=$tc2-$tc1;
$result[] = "Execution time for output buffering = $tc_a seconds. Output in $tc_b seconds<br>\n";
 
foreach($result as $res) echo $res;
 
wiki/discussion/performance/test.txt · Last modified: 2006/11/13 17:13 by andi
 
Imprint Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsTranslate