dtja73027 2014-11-11 15:19
浏览 71
已采纳

str_replace和str_ireplace之间是否存在性能差异? [关闭]

Is there a performance difference between str_replace and str_ireplace?

If yes, in favour of which function and why?

  • 写回答

2条回答 默认 最新

  • dongsong73032 2014-11-11 15:37
    关注

    str_ireplace imposes a little overhead because it needs to convert both "haystack" and "needle" to lowercase before comparison. (c source) However, since this conversion is ascii-only, it's lighting fast and won't affect performance in any noticeable way.

    Here's a little test:

    for($i = 2; $i < 7; $i++) {
        $x = str_repeat('a', pow(10, $i));
        $t = microtime(1); str_replace ('a', 'b', $x); $a = microtime(1) - $t;
        $t = microtime(1); str_ireplace('A', 'b', $x); $b = microtime(1) - $t;
        $t = microtime(1); strtolower($x);             $c = microtime(1) - $t;
        printf("%d replace=%.4f ireplace=%.4f lower=%.4f
    ", $i, $a, $b, $c);
    
    }
    

    Results:

    2 replace=0.0000 ireplace=0.0000 lower=0.0000
    3 replace=0.0000 ireplace=0.0000 lower=0.0000
    4 replace=0.0002 ireplace=0.0003 lower=0.0001
    5 replace=0.0021 ireplace=0.0030 lower=0.0008
    6 replace=0.0253 ireplace=0.0441 lower=0.0110
    

    So, for a string of 1,000,000 characters str_ireplace is only 0.02 sec "slower". My suggestion is to optimize other parts of your program first ))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多