Home  >  Article  >  Backend Development  >  PHP does not use regular collection speed research summary_PHP tutorial

PHP does not use regular collection speed research summary_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:53:44762browse


Note: All functions below do not use regular expressions.

The above is to extract the first matching three functions to achieve the same purpose

Copy the code The code is as follows:

function str_cut($str,$start, $end) {//Get the first match, the most efficient, split it first and then replace it
$content = strstr($str, $start);
$content = substr( $content, strlen( $start ), strpos( $content, $end ) - strlen( $start ) );
return $content;
}
function str_cut1( $str ,$start, $end) {//Get the first match, efficient, search and replace directly
$x = strpos($str, $start);
return substr($str, $x +strlen($start), strpos($str, $end)-$x+strlen($end));
}

function str_cut3($content,$start,$end){/ / Take out the first match. The larger the string, the slower it is!
$my = explode($start,$content);
$my = explode($end,$my[1]);
return $my[0];
}

The following is to extract all three matching functions (all original) to achieve the same collection purpose
Copy the code The code is as follows:

function strcut($str,$start, $end) //Search the number first, the speed is medium
{
if( strpos( $str, $start))
{
$sum = substr_count($str,$start);

$carr = array();
for($i=0;$i<$sum;$i++){
$str = strstr($str, $start);
$str = substr($str, strlen($start));
$carr[] = substr($str, 0, strpos($str, $end ) );
                                               $end ,$carr=array()) //Recursive, the slowest running efficiency!
{
if( strpos( $str, $start) )
{
$str = strstr($str, $start);
$str = substr($str, strlen( $start));
$carr[] = substr($str, 0, strpos($str, $end)); 🎜 > Return Str_cut_all ($ Str, $ Start, $ End, $ Carr);
}

}
Return $ Carr; ,$start,$end){//Get all matches, the fastest efficiency, because it is only read once, the larger the string, the more obvious it is
$m = explode($start,$content);
$a = array ();
for( $i = 1;$i < count($m);$i++ )
{
$my = explode($end,$m[$i]);
$a[] = $my[0];
unset($my);
}
return $a;
}


Note my-Ca Compare
if written like this:



Copy code

The code is as follows:


function my_Ca($content,$start,$ end){//Get all matches
$m = explode($start,$content);
$a = array();
$sum = count($m);
for( $i = 1;$i < $sum;$i++ )
{
$my = explode($end,$m[$i]);
$a[] = $my[0 ]; unset($my); } return $a; }

The speed is faster again!


As can be seen from the above, it is not that the array processing function (explode) is slower than the string processing function (substr, etc.), nor is it faster than this, because when matching multiple data, matching The greater the array function, the greater the advantage. Processing strings is like cutting a cake into smaller pieces. Matching a single string using the cut method has the same effect (str_cut). The key still lies in---algorithm! The algorithm is well written, and all functions are the same!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318650.htmlTechArticleNote: All functions below do not use regular expressions. The above is to extract the first matching three functions to achieve the same purpose. Copy the code. The code is as follows: functionstr_cut($str,$start,$en...
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