Efficiency Comparison of several php string connections (detailed description), php string

Source: Internet
Author: User

Efficiency Comparison of several php string connections (detailed description), php string

Php has three types of string connections:

1. directly use. For connection.

2. Connect with. =.

3. Press in the array first, and then join through the join function.

The efficiency of the three methods is tested as follows:

The code for the first method is as follows:

<?php      function get_tm() {   list ( $usec, $sec ) = explode ( " ", microtime () );   return (( float ) $usec + ( float ) $sec);   }      $temp="test";   $result="";   define("num",100000);   $start=get_tm();     for($i=0;$i<num;$i++)   {     $result=$result.$temp;   }   echo get_tm()-$start;   ?>

Run four times, except for the first run time, the three times are:

22.165272951126
22.003527164459
22.15947508812

The code for the second method is as follows:

<?php        function get_tm() {   list ( $usec, $sec ) = explode ( " ", microtime () );   return (( float ) $usec + ( float ) $sec);   }      $temp="test";   $result="";   define("num",100000);   $start=get_tm();     for($i=0;$i<num;$i++)   {     $result.=$temp;   }   echo get_tm()-$start;   ?>

Run four times, except for the first run time, the three times are:

3.1967310905457
3.1296961307526
3.0872850418091

The code for the third method is as follows:

<?php         function get_tm() {   list ( $usec, $sec ) = explode ( " ", microtime () );   return (( float ) $usec + ( float ) $sec);   }      $temp="test";   $result="";   $arr=array();   define("num",100000);   $start=get_tm();     for($i=0;$i<num;$i++)   {     array_push($arr, $temp);   }   $result=join($arr);   echo get_tm()-$start;   ?>

Run four times, except for the first run time, the three times are:

3.3184430599213
3.2759411334991
3.2663381099701

As can be seen from the above, it is the most inefficient to directly connect strings through.

The Efficiency Comparison (detailed description) of the above several php string connections is all the content shared by the editor. I hope to give you a reference, and I hope you can also support the help house.

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.