简体   繁体   English

PHP:字符串concat和缓冲内容之间的性能差异

[英]PHP: Performance difference between string concat and buffering contents

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 字符串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. 看起来输出缓冲+ echo始终更快,至少在我的机器上的CLI /中。

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. 然而,Brendan Long在他的评论中提出了一个很好的观点 - 这里有一个小的性能差异,选择一个或另一个并不是一个问题。

I've made a benchmark, using OB is faster. 我做了一个基准测试,使用OB更快。

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. op_start用于缓冲输出。 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. ob end将从脚本发送输出,缓冲区将被清除。

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. 如果你以后不需要对输出做任何事情,只需使用echo并释放内存。 Don't use unnecessary variables. 不要使用不必要的变量。

another advantage of ob_start is that you can use it with a callback. ob_start的另一个优点是你可以将它与回调一起使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM