Which is more efficient, PHP string functions or regex in PHP?

后端 未结 9 1845
甜味超标
甜味超标 2020-12-06 16:48

I\'m writing PHP code to parse a string. It needs to be as fast as possible, so are regular expressions the way to go? I have a hunch that PHP string functions are more expe

相关标签:
9条回答
  • 2020-12-06 17:09

    I believe there is a threshold from which a regular expression is faster than a bunch of PHP string function calls. Anyway, depends a lot on what you're doing. You have to find out the balance.

    Now that you edited your question. I'd use string functions for what you're trying to accomplish. strpos() and substr() is what comes to mind at a first glance.

    0 讨论(0)
  • 2020-12-06 17:11

    Depends on your needs. Most regular expression operations are faster than one would think and can even outperform builtin string functions in certain trivial operations. Note that I have the preg library in mind, not the builtin regex library, which is quite slow.

    0 讨论(0)
  • 2020-12-06 17:13

    In general, string functions are faster and regex functions are more flexible.

    As with anything else, your results may vary, the only way to know for sure is to try it both ways and benchmark.

    0 讨论(0)
  • 2020-12-06 17:16

    I think if you want highest performance, you should avoid regex as it helps to minimize effort, but won't have the best performance as you can almost always adjust code using string routines to a specific problem and gain a big performance boost of it. But for simple parsing routines that can't be optimized much, you can still use regex as it won't make a big difference there.

    EDIT: For this specific problem you posted I'd favorize string operations, but only because I wouldn't know how to do it in regex. This seems to be pretty straight-forward, except for the hash, so I think regex/string functions won't make a big difference.

    0 讨论(0)
  • 2020-12-06 17:18

    I was searching some information about regex performance - as I need to do a lot of lookups - and truth is that is depends on what you want to achieve. For my purpose I tested one type of searching to compare performance.

    Specification: I need to find simple string in array of strings. To test I have $testArray which is array of ~11k multi word phrases build from article about Tolkien (eg. strings "history of the lord of the rings", "christopher tolkien"). As I want to find only phrases containing exact word I cant use strpos() function as eg. when searching for "ring" it would find also phrases with "ringtone" word.

    Code using php functions:

    $results = array();
    $searchWord = 'rings';
    foreach ($testArray as $phrase){
      $phraseArr = explode(' ', $phrase);
      if(in_array($searchWord, $phraseArr)){
        $results[] = $phrase;
      }
    }
    

    Code using regex function:

    $results = array();
    $pattern= "/( |^)rings( |$)/";
    $results = preg_grep($pattern, $testArray);
    

    I found out in this case regex function was around 10 times faster

    Execution times for 100 searches was (using various words)

    • from 0.3436 to 0.3468 second for php functions
    • from 0.0332 to 0.0406 second for regex

    Such searching might be trival, but for more complex tasks I assume it would be extremely hard/impossible to implement it without regex just on native php functions.

    In conclusion: for simple tasks you should use regex beacuse it would propably be faster, and for complex tasks you propably have to use regex beacuse it would be only way to solve problem.

    Edit:

    I just realize that this topic is about "PHP string functions" and my test code uses explode() and in_array() functions. So I tried other approach. As my delimiter is space search method below also works and uses strpos() function.

    Code using strpos() function:

    $results = array();
    $searchWord = 'rings';
    foreach ($testArray as $phrase){
      if(strpos(' ' . $phrase . ' ', ' ' . $searchWord . ' ')!==FALSE){
        $results[] = $phrase;
      }
    } 
    

    But still results was a lot worse than in regex case.

    So performance summary is:

    • from 0.3436 to 0.3468 second for php array functions
    • from 0.2001 to 0.2273 second for strpos() function
    • from 0.0332 to 0.0406 second for regex

    Still regex is a big winner.

    0 讨论(0)
  • 2020-12-06 17:20

    I agree with everybody: string functions a a bit more performant, than regex functions. I just wanted to show a small test, that I did in terminal as a proof:

    strpos():

    $ time php -r '$i = 0; while($i++ < 1000000) strpos("abc", "a");'
    
    real    0m0.380s
    user    0m0.368s
    sys    0m0.008s
    

    preg_match():

    $ time php -r '$i = 0; while($i++ < 1000000) preg_match("/abc/", "a");'
    
    real    0m0.441s
    user    0m0.432s
    sys    0m0.004s
    
    0 讨论(0)
提交回复
热议问题