fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

compare gsub vs tr vs delete to remove character from string #121

Closed mojavelinux closed 1 year ago

mojavelinux commented 7 years ago

This benchmark proves that delete is faster than tr and both are considerably faster than gsub when removing a character from a string.

mojavelinux commented 7 years ago

Is there anything I need to do to get this PR accepted?

mojavelinux commented 1 year ago

Done.

I ran the tests on 2.2.0 since that's what the README says is being used, but the performance is even more noticable using Ruby 3.2.1.

Comparison:
String#delete (const): 7168876.7 i/s
       String#delete:  6308819.2 i/s - 1.14x  slower
           String#tr:  5156099.1 i/s - 1.39x  slower
         String#gsub:  1337592.2 i/s - 5.36x  slower