Fixed length of the string (the hypothesis is 06481a63041b578d702f159f520847f8), to the fixed format for cutting, using PHP how to cut faster?
Note: To cut this string into the three string 06/48/1a63041b578d702f159f520847f8.
Write a simple program to do a test to compare the speed of substr/sscanf/preg_match.
First $a = ' 06481a63041b578d702f159f520847f8 ', and then perform the following program test (all run 100 Wanji):
- It takes 4.08482551575 seconds to execute with the SUBSTR program:
$x [0] = substr ($a, 0, 2);
$x [1] = substr ($a, 2, 2);
$x [2] = substr ($a, 4, 28);
- Spend 5.26213645935 seconds with the SSCANF program (with substr-5.26213645935)
List ($x [0], $x [1], $x [2]) = SSCANF ($a, '%2s%2s%28s ');
Note: $x = sscanf ($a, '%2s%2s%28s '); The time consuming time increases to 8 seconds, will be slower than Preg_match.
- Spend 7.58504867554 seconds with the Preg_match program (with substr difference:-7.58504867554)
Preg_match ('/^ (\w{2}) (\w{2}) (\w{28}) $/', $a, $match);
$x [0] = $match [1];
$x [1] = $match [2];
$x [2] = $match [3];
In conclusion, substr () to win the victory, or not lazy lazy, although to write more lines, but the speed will be much faster.
PHP split string Function speed ratio (substr/sscanf/preg_match)---substr fastest!