首页/Home PHP Bench/Performance Echo: Comma VS Concatenation

Echo: Comma VS Concatenation

PrintE-mail
Wednesday, 27 August 2008 19:05  

This page talks about the "echo" performance.

Usually, we use concatenation to output strings. After reading this article, probably you will try to avoid it.

The test page tries to use 20, 000 iterations to generate the results. The code used is as follows:

<?php
ob_start
();

$iterations 20000;

$testString md5('test');

$start microtime(true);

for (
$i 0$i $iterations$i++) {
    echo 
$testString$testString$testString' ';
}

$end1 microtime(true);

for (
$j 0$j $iterations$j++) {
    echo 
$testString$testString$testString' ';
}

$end2 microtime(true);

ob_end_clean();

echo 
'<pre>',
     
'Iteration:     'number_format($iterations), PHP_EOLPHP_EOL,
     
'Time taken (in millisecond):'PHP_EOL,
     
'Concatenation: ', (($end1 $start) * 1000), PHP_EOL,
     
'Comma:         ', (($end2 $end1) * 1000), PHP_EOL,
     
'</pre>';
?>

 

You can check the result here.

If you refresh that page 5 or 10 times, then you can come to your own choice.

 

回复

留个脚印儿吧.


回复