`str.count("\n")` is 1.3-170 times faster than `str.lines.count` or `str.each_line.count` depending on the string size
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 45/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- ruby
- Domain
- performance
Research direction
Start by inspecting fast-ruby's existing benchmark organization and compare it with the benchmark code shown in tmp/string_count_benchmark.rb. Add the String line-count comparison in the repository's established format, then run the relevant benchmark or project checks to confirm the new entry works.
Written by the indexing model from the issue text.
Description
str.count("\n") is 1.3-170 times faster than str.lines.count or str.each_line.count (ref: https://serpapi.com/blog/lines-count-failed-deployments/). The speed difference grows with the lines count.
$ ruby tmp/string_count_benchmark.rb
Warming up --------------------------------------
String#count('\n') 86.000 i/100ms
String#lines.size 1.000 i/100ms
String#lines.count 1.000 i/100ms
String#each_line.count
1.000 i/100ms
Calculating -------------------------------------
String#count('\n') 771.031 (± 6.6%) i/s - 3.870k in 5.041849s
String#lines.size 4.785 (± 0.0%) i/s - 24.000 in 5.037242s
String#lines.count 4.513 (± 0.0%) i/s - 23.000 in 5.112095s
String#each_line.count
4.763 (± 0.0%) i/s - 24.000 in 5.075882s
Comparison:
String#count('\n'): 771.0 i/s
String#lines.size: 4.8 i/s - 161.12x (± 0.00) slower
String#each_line.count: 4.8 i/s - 161.87x (± 0.00) slower
String#lines.count: 4.5 i/s - 170.86x (± 0.00) slower
Benchmark code:
require "benchmark/ips"
HTML = "\nruby\n" * 1024 * 1024
def fastest
HTML.count("\n")
end
def faster
HTML.each_line.count
end
def fast
HTML.lines.length
end
def slow
HTML.lines.size
end
Benchmark.ips do |x|
x.report("String#count('\\n')") { fastest }
x.report("String#lines.size") { faster }
x.report("String#lines.count") { fast }
x.report("String#each_line.count") { slow }
x.compare!
end
I'd like to add this benchmark to fast-ruby. Wdyt?
Based on our updates to the @guilhermesimoes' very helpful gist: https://gist.github.com/guilhermesimoes/d69e547884e556c3dc95?permalink_comment_id=4687645#gistcomment-4687645
- Dominant language
- Ruby
- Stars
- 5.7k
- Forks
- 370
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fastruby/fast-ruby
-
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
-
Difficulty 4/5 3-5 days Newbie friendliness 32/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
All issues in fastruby/fast-ruby
Similar issues
-
SyncEm always forwards a dummy block, so wrapped methods lose their no-block/Enumerator behavior Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug P2
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
dependabot
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
githubnext/gh-aw-cao#13349 ·
-
user-reported
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Kong/developer.konghq.com#7316 ·