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

ADD: example for String#sub vs String#slice! #119

Closed roxer closed 2 years ago

ixti commented 7 years ago

There's no point in comparing bang methods VS non-bang. As slice! changes string in-place. Also comparing sub to slice is kinda strange. sub replaces substring matching regexp with the other string, and slice instead returns matching substring

jonathanhefner commented 7 years ago

As @ixti said, sub vs slice is a bit odd. It would be helpful to know what end result you're trying to achieve before deciding which methods to compare. Also, your fastest test case is labeled as String#[integer, integer] but it is really String#[Range] (the two are different in terms of semantics).

If you do want to compare bang methods (which involve mutation), I wrote a gem that can help with that: benchmark-inputs. You can use it just like benchmark-ips, and it will provide similar output. Let me know if you have any questions.