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.