PHP string single-character gas Festival-substr really slow

Source: Internet
Author: User
PHP string single-byte operations -- substr is really slow. what kind of operations should we use when we want to operate a single-byte node in a string? I always thought that the substr function would not be very slow. But I tested it today, but this is not the case. The following is the test code :? & Lt ;? Php $ str = 'abcdefghijklmnopqrstu PHP string single-byte operation -- substr is really slow

What method should we use to operate a single word segment in a string? I always thought that the substr function would not be very slow. But I tested it today, but this is not the case. The following is the test code:

?

 0; $ k --) {for ($ I = 0; $ I
 









T I m e: ". round ($ end-$ start, 6 )."










"); // Use byte operations $ start = microtime_float (); for ($ k = 100; $ k> 0; $ k --) {for ($ I = 0; $ I









T I m e: ". round ($ end-$ start, 6 )."










"); // Use array mode to operate $ start = microtime_float (); for ($ k = 100; $ k> 0; $ k --) {for ($ I = 0; $ I









T I m e: ". round ($ end-$ start, 6 )."










");?>

? The test results are as follows:

T I m e: 0.003782
T I m e: 0.002862
T I m e: 0.002587

T I m e: 0.00351
T I m e: 0.002649
T I m e: 0.002786

It can be seen that the speed of substr is very slow. The byte operation mode is the same as the array operation mode. Therefore, we believe that if you perform byte operations on strings, the byte operation mode is the best, because this method will not mistakenly assume that the variable type is an array.
Of course, this operation method has its limitations, that is, it cannot be processed for Chinese strings. you only need to use mb_substr !!

Related Article

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.