PHP strtr vs str_replace benchmarking

前端 未结 3 1284
误落风尘
误落风尘 2020-12-21 11:34

I\'m curious what the most performant method of doing string transformations is. Given a n input string and a set of translations, what method is the most efficient in gene

3条回答
  •  长情又很酷
    2020-12-21 12:01

    I made a trivial benchmark for personal needs on the two functions. The goal is to change a lowercase 'e' to an uppercase 'E'.

    
    

    This code using str_replace runs in 6 seconds. Now the same with the strtr function :

    
    

    It took only 4 seconds.

    So as stated by T0xicCode, for this particularly simple case, strtr is indeed faster than str_replace, but the difference is not so significant.

提交回复
热议问题