Home  >  Article  >  Backend Development  >  PHP有没有必要实现stringbuilder解决思路

PHP有没有必要实现stringbuilder解决思路

WBOY
WBOYOriginal
2016-06-13 12:04:54968browse

PHP有没有必要实现stringbuilder
本人刚刚毕业,也是从C#转来,在学习PHP的过程中发现PHP没有类似C#一样的stringbuilder类型。
因为我知道在C#中多次操作字符串用stringbuilder的效率要高过string,那么想问下PHP为什么没有实现stringbuilder?
还有,如果我自己用数组模拟stringbuilder的话会提高效率么?
------解决方案--------------------
字符串比數組模拟快多了。
字符串

<br />$starttime = getMicrotime();<br />$str = '';<br />for($i=0;$i<100000;$i++){<br />    $str .= $i;<br />}<br />$endtime = getMicrotime();<br /><br />printf("run time %f ms\r\n", (float)($endtime-$starttime)*1000); <br /><br />function getMicrotime(){<br />    list($usec, $sec) = explode(' ', microtime());<br />    return (float)$usec + (float)$sec;<br />}<br />

run time 61.100006 ms

数组
<br />$starttime = getMicrotime();<br />$arr = array();<br />for($i=0;$i<100000;$i++){<br />    array_push($arr, $i);<br />}<br />$endtime = getMicrotime();<br /><br />printf("run time %f ms\r\n", (float)($endtime-$starttime)*1000);<br /><br />function getMicrotime(){<br />    list($usec, $sec) = explode(' ', microtime());<br />    return (float)$usec + (float)$sec;<br />}<br />

run time 200.176954 ms

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn