Php does not need regular expression collection speed inquiry summary _ PHP Tutorial

Source: Internet
Author: User
Php does not need regular expression collection speed inquiry summary. Note: the following functions do not use regular expressions. The above code is used to retrieve the first matching three functions for the same purpose and copy the code as follows: functionstr_cut ($ str, $ start, $ en note: All the following functions do not use regular expressions.

The above are the three functions that match the first one to achieve the same purpose.

The code is as follows:


Function str_cut ($ str, $ start, $ end) {// obtain the first match, with the highest efficiency. split the string 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) {// retrieve the first match. in efficiency, directly search for replacement.
$ X = strpos ($ str, $ start );
Return substr ($ str, $ x + strlen ($ start), strpos ($ str, $ end)-$ x + strlen ($ end ));
}

Function str_cut3 ($ content, $ start, $ end) {// retrieve the first match. the larger the string, the slower the speed!
$ My = explode ($ start, $ content );
$ My = explode ($ end, $ my [1]);
Return $ my [0];
}


The following are all the three matched functions (all original functions) for the same collection purpose:

The code is as follows:


Function strcut ($ str, $ start, $ end) // The number of first searches, with medium speed
{
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 ));
}



}
Return $ carr;
}

Function str_cut_all ($ str, $ start, $ end, $ carr = array () // recursion, the slowest running efficiency!
{
If (strpos ($ str, $ start ))
{
$ Str = strstr ($ str, $ start );
$ Str = substr ($ str, strlen ($ start ));
$ Carr [] = substr ($ str, 0, strpos ($ str, $ end ));
If (strpos ($ str, $ start ))
{
Return str_cut_all ($ str, $ start, $ end, $ carr );
}

}
Return $ carr;
}

Function my_Ca ($ content, $ start, $ end) {// retrieve all the matches, which is the fastest efficiency. because the value is read-only once, the larger the string, the more obvious
$ M = explode ($ start, $ content );
$ A = array ();
For ($ I = 1; $ I <count ($ m); $ I ++)
{
$ My = explode ($ end, $ m [$ I]);
$ A [] = $ my [0];
Unset ($ my );
}
Return $;
}


Note my-Ca comparison
Write the following statement:

The code is as follows:


Function my_Ca ($ content, $ start, $ end) {// retrieve 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 $;
}


Faster!


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.), or that is faster than this, because the array function has more advantages when matching multiple data, the smaller the string to be processed, the smaller the slice. Matching a single string using the cut idea is also the same effect (str_cut ). The key is --- algorithm! The algorithms are well written, and all functions are the same!

Bytes. The above code extracts the first matching three functions to achieve the same purpose: functionstr_cut ($ str, $ start, $ en...

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.