|
|
|
|
|
by wlll
3278 days ago
|
|
When I've benchmarked regexes before (Ruby ISTR) there have been situations where the regex engine optimised the code to be comparable to string functions, I can't remember the details though. Just for fun I benchmarked =~ /\Asomething/ and start_with?("something"), a situation that seems could be optimisable by the regex engine, but the string function is still faster (Ruby 2.3.1): start_with: 5703143.1 i/s
regex: 2821224.4 i/s - 2.02x slower That aside: > There's no reason to contort your code around something that should automatically happen. Absolutely, but it's a question of style at that point. "Contorting your code" suggests using a less desirable style/syntax for some gain, and I'd agree would be premature optimisation. If you're just making a choice between two styles that you consider to be pretty much equal then it's just pragmatic. edit: Forgot to add, yes, I agree that this should happen automatically in the compiler :) |
|