PHP split string Function speed ratio (substr/sscanf/preg_match)---substr fastest!

Source: Internet
Author: User

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!

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.