Is it necessary for PHP to implement stringbuilder? I just graduated, but I also switched from C #. when I learned PHP, I found that PHP has no stringbuilder type similar to C.
Because I know that the efficiency of using stringbuilder to operate strings multiple times in C # is higher than that of string, why does PHP not implement stringbuilder?
Also, will it improve efficiency if I use arrays to simulate stringbuilder?
Reply to discussion (solution)
You can test it on your own. it doesn't make sense that it will occupy higher memory overhead.
String ratio ?? The simulation is much faster.
String
$starttime = getMicrotime();$str = '';for($i=0;$i<100000;$i++){ $str .= $i;}$endtime = getMicrotime();printf("run time %f ms\r\n", (float)($endtime-$starttime)*1000); function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec;}
Run time 61.100006 MS
Array
$starttime = getMicrotime();$arr = array();for($i=0;$i<100000;$i++){ array_push($arr, $i);}$endtime = getMicrotime();printf("run time %f ms\r\n", (float)($endtime-$starttime)*1000);function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec;}
Run time 200.176954 MS
I found the principle explanation:
All the variables in PHP are in the same structure as Zval at the underlying layer. The Zval structure shows that in PHP, strings are composed of pointers and length structs pointing to actual data, which is similar to strings in c ++. Because an actual variable represents the length, different from c, its string can be binary data (including \ 0), and in PHP, evaluate the string length. strlen is an O (1) operation.
When adding, modifying, and appending strings, PHP will re-allocate the memory to generate new strings. Finally, for security reasons, \ 0 is still added at the end of a string generated by PHP.
Comparison of common string concatenation methods and speed:
Suppose there are four variables: $ strA = '000000'; $ strB = '000000'; $ intA = 123; intB = 456;
Now we will compare and describe the following string concatenation methods:
1 $ res = $ strA. $ strB and $ res = "$ strA $ strB"
2 in this case, zend will re-malloc a piece of memory and perform corresponding processing, the speed is normal
3 $ strA = $ strA. $ strB
4. this is the fastest speed. zend will directly relloc the current strA to avoid duplicate copies.
5 $ res = $ intA. $ intB
6. this speed is slow. because implicit format conversion is required, you should avoid it as much as possible in actual programming.
7 $ strA = sprintf ("% s", $ strA. $ strB );
8. this is the slowest way, because sprintf is not a language structure in PHP, and it takes a lot of time for format recognition and processing, and the mechanism is also malloc. However, the sprintf method is the most readable and can be flexibly selected based on actual conditions.
[PS: the implementation of the string type in PHP is actually the stringbuilder in C # and Java, during its own expansion, it does not re-allocate the memory value, but dynamically allocates the memory based on the existing memory through malloc. Therefore, although the string usage in PHP is similar to that in C # and Java, however, it is stringbuilder in principle]
String ratio ?? The simulation is much faster.
String
$starttime = getMicrotime();$str = '';for($i=0;$i<100000;$i++){ $str .= $i;}$endtime = getMicrotime();printf("run time %f ms\r\n", (float)($endtime-$starttime)*1000); function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec;}
Run time 61.100006 MS
Array
$starttime = getMicrotime();$arr = array();for($i=0;$i<100000;$i++){ array_push($arr, $i);}$endtime = getMicrotime();printf("run time %f ms\r\n", (float)($endtime-$starttime)*1000);function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec;}
Run time 200.176954 MS
Thanks again, I also wrote code to explain it to me.