You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
str.count("\n") is 1.3-170 times faster than str.lines.count or str.each_line.count depending on the string size #220
changed the title [-]`str.count($/)` is 1.3x faster than `str.lines.count` or `str.each_line.count`[/-][+]`str.count("\n")` is 1.3x faster than `str.lines.count` or `str.each_line.count`[/+]on Nov 21, 2023
changed the title [-]`str.count("\n")` is 1.3x faster than `str.lines.count` or `str.each_line.count`[/-][+]`str.count("\n")` is 1.3-170 times faster than `str.lines.count` or `str.each_line.count` depending on the string size[/+]on Nov 21, 2023
Activity
ilyazub commentedon Oct 26, 2023
@JuanVqz @etagwerker what do you think about a benchmark for
String#countvsString#lines.countvsString#each_line.count?JuanVqz commentedon Oct 26, 2023
This seems a Rails related benchmark, I wonder if we are adding framework-related benchmarks
ilyazub commentedon Oct 27, 2023
It doesn't depend on Rails (ruby/ruby#4001 (comment)). I updated the benchmark code to work with the plain Ruby.
ilyazub commentedon Oct 31, 2023
@JuanVqz I updated the benchmark code above to work with the plain Ruby.
What do you think?
ixti commentedon Nov 21, 2023
IMHO using
$/is bad - it's less obvious than"\n". More than that,$/can be redefined, so the code may become broken.[-]`str.count($/)` is 1.3x faster than `str.lines.count` or `str.each_line.count`[/-][+]`str.count("\n")` is 1.3x faster than `str.lines.count` or `str.each_line.count`[/+]ilyazub commentedon Nov 21, 2023
@ixti Sounds good. The performance difference is in the
String#countvs other methods.$/vs\ndid't impact performance here.[-]`str.count("\n")` is 1.3x faster than `str.lines.count` or `str.each_line.count`[/-][+]`str.count("\n")` is 1.3-170 times faster than `str.lines.count` or `str.each_line.count` depending on the string size[/+]