PHP has no need to implement StringBuilder

Source: Internet
Author: User
I just graduated, but also from C #, in the process of learning PHP found that PHP is not similar to C # StringBuilder type.
Because I know that in C # multiple manipulation of strings with StringBuilder efficiency is higher than string, then want to ask the next PHP why not implement StringBuilder?
Also, if I use an array to simulate StringBuilder, will it improve efficiency?


Reply to discussion (solution)

You can test it yourself, it doesn't make any difference, it's not worth the expense of having a higher memory cost.

String ratio?? The simulation is much faster.
String

$starttime = Getmicrotime (), $str = ", for ($i =0; $i <100000; $i + +) {    $str. = $i;} $endtime = Getmicrotime ();p rintf ("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 ();p rintf ("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

All the variables in PHP are the same structure zval at the bottom. The ZVAL structure shows that in PHP, strings are made up of pointers and length structures that point to actual data, which is similar to string in C + +. Since the length is represented by an actual variable, and unlike C, its string can be 2 binary (inclusive), while in PHP, the string length strlen is an O (1) operation.
when adding, modifying, appending string operations, PHP will reallocate memory to generate a new string. Finally, for security reasons, PHP will still add the "." At the end of a string generation.
Common string stitching method and Speed comparison:

Assume that there are 4 variables: $strA = ' 123 '; $strB = ' 456 '; $intA =123; intb=456;

Now makes a comparison and description of the following string concatenation methods:
1 $res = $strA. $strB and $res = "$strA $strb"
2 in this case, the Zend will re-malloc a piece of memory and handle it accordingly, the speed General
3 $strA = $strA. $strB
4 This is the fastest, Zend will relloc directly on the current stra basis, avoiding duplicate copies of
5 $res = $intA. $intB
6 This is slower because it needs to be Implicit format conversion, the actual writing program should also be careful to avoid
7 $strA = sprintf ("%s%s", $strA. $strB);
8 This is the slowest way, because sprintf is not a language structure in PHP, it takes more time to recognize and process the format itself, and the mechanism itself is malloc. However, the sprintf is the most readable, in practice can be flexibly selected according to the specific circumstances.


PS: Because the implementation of the string type in PHP is StringBuilder in C # and Java itself, it does not reallocate memory assignments on its own, but dynamically allocates memory on existing memory based on malloc. So string in PHP is similar to string usages in C # and Java, but is StringBuilder "

String ratio?? The simulation is much faster.
String

$starttime = Getmicrotime (), $str = ", for ($i =0; $i <100000; $i + +) {    $str. = $i;} $endtime = Getmicrotime ();p rintf ("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 ();p rintf ("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


Same thanks, and I wrote the code to explain.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.