开发者

PHP: Performance difference between string concat and buffering contents

开发者 https://www.devze.com 2023-03-25 07:44 出处:网络
I do lots of string concatenation in code and then I display output. I was wondering if there is any difference between following two codes:

I do lots of string concatenation in code and then I display output. I was wondering if there is any difference between following two codes:

string concat

$str = '';
for($x =0; $x <= 10000; $x++):
    $str .= 'I am string '. $x . "\n";
endforeach;

Output buffering

 ob_start();
 for($x =0; $x <= 10000; $x++):
    echo 'I am string ', $x , "\n";
 endforeach;
 $str = ob_get_contents();
 ob_end_开发者_如何转开发flush();


Here's a benchmark for you:

<?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';

?>

My results:

$ 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%

Looks like output buffering + echo is consistently faster, at least in the CLI / on my machine.

Brendan Long made a good point in his comment, however — there is such a minor performance difference here that a choice of one or the other is not much of an issue.


I've made a benchmark, using OB is faster.

String: 0.003571 seconds
Output Buffer: 0.003053 seconds
StringBuilder (custom class Java/C#-Like): 0.050148 seconds
Array and Implode: 0.006248 seconds


at the first codes you write, you assign a string which will be kept until you unset the variable. It will stay in memory.

at the second one; op_start is to buffer the output. Until you end it, it will be stored in a buffer. ob end will send the ouput from the script and buffer will be cleaned.

instead of using variables or another thing; if you dont need to do anything with the output later, just use echo and free the memory. Don't use unnecessary variables.

another advantage of ob_start is that you can use it with a callback.

See here http://php.net/manual/en/function.ob-start.php

0

精彩评论

暂无评论...
验证码 换一张
取 消