Вот вам эталонный тест:
<?php
$test_1_start = microtime();
$str = '';
for ( $x = 0; $x <= 10000; $x++ ) {
$str .= 'I am string ' . $x . "\n";
}
$test_1_end = microtime();
unset($str);
echo 'String concatenation: ' . ( $test_1_end - $test_1_start ) . ' seconds';
$test_2_start = microtime();
ob_start();
for ( $x = 0; $x <= 10000; $x++ ) {
echo 'I am string ', $x, "\n";
}
$str = ob_get_contents();
ob_end_clean();
$test_2_end = microtime();
echo "\nOutput buffering: " . ( $test_2_end - $test_2_start ) . ' seconds';
?>
Мои результаты:
$ php -v
PHP 5.3.4 (cli) (built: Dec 15 2010 12:15:07)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
$ php test.php
String concatenation: 0.003932 seconds
Output buffering: 0.002841 seconds%
$ php test.php
String concatenation: 0.004179 seconds
Output buffering: 0.002796 seconds%
$ php test.php
String concatenation: 0.006768 seconds
Output buffering: 0.002849 seconds%
$ php test.php
String concatenation: 0.004925 seconds
Output buffering: 0.002764 seconds%
$ php test.php
String concatenation: 0.004066 seconds
Output buffering: 0.002792 seconds%
$ php test.php
String concatenation: 0.004049 seconds
Output buffering: 0.002837 seconds%
Похоже, буферизация вывода + echo
постоянно быстрее, по крайней мере в CLI / на моемmachine.
Брендан Лонг сделал хороший комментарий в своем комментарии, однако - здесь такая незначительная разница в производительности, что выбор того или другого не является большой проблемой.