which is the fast process strpos()/stripos() or preg_match() in php

前端 未结 3 1227
半阙折子戏
半阙折子戏 2021-01-01 05:16

I just want to known which one will be fast of strpos()/stripos() or preg_match() functions in php.

相关标签:
3条回答
  • 2021-01-01 06:00

    preg_match is the slowest of the three. stripos will be slower than strpos, since it will have to do extra work to handle case-insensitive matching.

    0 讨论(0)
  • 2021-01-01 06:07

    I found this blog that has run some testes regarding your question, the result was:

    • strpos() is 3-16 times faster than preg_match()
    • stripos() is 2-30 times slower than strpos()
    • stripos() is 20-100 percent faster than preg_match() with the caseless modifier "//i"
    • using a regular expression in preg_match() is not faster than using a long string
    • using the utf8 modifier "//u" in preg_match() makes it 2 times slower

    The code used was:

    <?php
    
    function loop(){
    
    $str_50 = str_repeat('a', 50).str_repeat('b', 50);
    $str_100 = str_repeat('a', 100).str_repeat('b', 100);
    $str_500 = str_repeat('a', 250).str_repeat('b', 250);
    $str_1k = str_repeat('a', 1024).str_repeat('b', 1024);
    $str_10k = str_repeat('a', 10240).str_repeat('b', 1024);
    $str_100k = str_repeat('a', 102400).str_repeat('b', 1024);
    $str_500k = str_repeat('a', 1024*500).str_repeat('b', 1024);
    $str_1m = str_repeat('a', 1024*1024).str_repeat('b', 1024);
    
    $b = 'b';
    $b_10 = str_repeat('b', 10);
    $b_100 = str_repeat('b', 100);
    $b_1k = str_repeat('b', 1024);
    
    echo str_replace(',', "\t", ',strpos,preg,preg U,preg S,preg regex,stripos,preg u,'.
      'preg i,preg u i,preg i regex,stripos uc,preg i uc,preg i uc regex').PHP_EOL;
    
    foreach (array($b, $b_10, $b_100, $b_1k) as $needle) {
      foreach (array($str_50, $str_100, $str_500, $str_1k, $str_10k,
        $str_100k, $str_500k, $str_1m) as $str) {
    
        echo strlen($needle).'/'.strlen($str);
    
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = strpos($str, $needle); // strpos
        echo "\t".mt($start);
    
        $regex = '!'.$needle.'!';
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg
        echo "\t".mt($start);
    
        $regex = '!'.$needle.'!U';
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg Ungreedy
        echo "\t".mt($start);
    
        $regex = '!'.$needle.'!S';
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg extra analysiS
        echo "\t".mt($start);
    
        $regex = "!b{".strlen($needle)."}!";
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg regex
        echo "\t".mt($start);
    
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = stripos($str, $needle); // stripos
        echo "\t".mt($start);
    
        $regex = '!'.$needle.'!u';
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg utf-8
        echo "\t".mt($start);
    
        $regex = '!'.$needle.'!i';
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg i
        echo "\t".mt($start);
    
        $regex = '!'.$needle.'!ui';
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg i utf-8
        echo "\t".mt($start);
    
        $regex = "!b{".strlen($needle)."}!i";
        $start = mt();
        for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg i regex
        echo "\t".mt($start);
    
        echo PHP_EOL;
      }
      echo PHP_EOL;
    }
    }
    
    function mt($start=null){
      if ($start === null) return microtime(true);
      return number_format(microtime(true)-$start, 4);
    }
    
    loop();
    
    0 讨论(0)
  • 2021-01-01 06:16

    Benchmarking is a tricky business, but it's fairly safe to say that preg_match is slower than strpos or stripos. This is because the PRCE functions implement a REGEX engine that is much more powerful and flexible than the string functions.

    They also do different things. strpos will tell you the index of the start of the string inside another string, whereas preg_match is mainly used to probe the format of a string, and to retrieve sections of it based on regular expressions.

    In short, if you simply want to find a string inside another string, use strpos or stripos.

    0 讨论(0)
提交回复
热议问题