Search code examples
phppreg-matchstrposstripos

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


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


Solution

  • 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();