dsij89625 2016-06-30 06:28
浏览 36

Php和正则表达式性能

I want to write a script to detect suffixes, words, and sections of written language. It will include a lot of string matching, so I want to write it based on regular expressions.

But I have a question about performance. For example, let's say we have 100+ different regular expressions and we want to match all of them simultaneously for an input text. How would that affect performance?

We have two options as I know, making an array of regular expressions and running them respectively with a loop. Another one is combining all the expressions with "|" and getting a huge regular expression string. Which one would be better by means of performance?

If none of these are logical, can you please suggest me another method to achieve that?

  • 写回答

1条回答 默认 最新

  • dsgd5756 2016-06-30 06:34
    关注

    I cannot tell you which is faster, but you can run both versions in this mini test suite (and tell us the results :D)

    $startA = microtime(true);
    for($i = 0; $i < 10000; $i++)
    {
        //First version
    }
    $endA = microtime(true);
    
    unset ($all, $your, $variables);
    
    $startB = microtime(true);
    for($i = 0; $i < 10000; $i++)
    {
        //Second version
    }
    $endB = microtime(true);
    
    echo $endA-$startA."<br />";
    echo $endB-$startB."<br />";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号