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 !!