output buffering vs string concatenation / ob vs 拼接字符串
2017-01-10 16:25
411 查看
点击打开链接http://stackoverflow.com/questions/6934762/php-performance-difference-between-string-concat-and-buffering-contents
<?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%
<?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%
相关文章推荐
- Educational Codeforces Round 9-C. The Smallest String Concatenation(字符串排序)
- The Smallest String Concatenation CodeForces 632C 字符串
- Use parameters instead of string concatenation for forming SQL queries,用参数方式来生成sql语句,而不是用连接字符串的方式
- Java String vs StringBuilder vs StringBuffer Concatenation Performance Micro Benchmark
- "String Concatenation vs String Builder - The performance hit! See it to believe it :o)"
- Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
- StringBuilder vs String concatenation in toString() in Java
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
- Javascript拼接字符串的效率比较:String.replace VS Array.push
- String字符串拼接,截取,查找及日期转换
- 字符串拼接时用StringBuffer代替String
- String字符串拼接原理剖析
- Java String字符串拼接,截取,查找及日期转换
- the evil cmake -- string concatenation
- c++的字符串char与string相互转化,以及string的,切割,替换字符,字符串拼接方法(object向c++转型系列教程2)
- appendChild、拼接string、join三种方式比较字符串拼接
- 字符串的拼接,StringBuffer性能上还是远远优于String的
- Efficient String Concatenation in Python
- Java从入门到精通 - JavaString 字符串拼接,截取,查找及日期转换
- a note about string Concatenation