Skip to content

Instantly share code, notes, and snippets.

@guilhermesimoes
Last active September 11, 2023 09:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save guilhermesimoes/d69e547884e556c3dc95 to your computer and use it in GitHub Desktop.
Save guilhermesimoes/d69e547884e556c3dc95 to your computer and use it in GitHub Desktop.
Ruby Benchmark: Counting the number of lines of a file
# gem install benchmark-ips
require "benchmark/ips"
path = "lib/rubycritic/cli/options.rb"
Benchmark.ips do |x|
x.report("read + each_line") { File.read(path).each_line.count }
x.report("open + each_line") { File.open(path, "r").each_line.count }
x.report("open + readlines") { File.open(path, "r").readlines.length }
x.report("foreach + count") { File.foreach(path).count }
x.report("foreach + inject") { File.foreach(path).inject(0) {|count, line| count+1} }
x.report("foreach") { count = 0; File.foreach(path) { count+=1}; count }
x.report("wc") { `wc -l < "#{path}"`.to_i }
x.compare!
end
Calculating -------------------------------------
read + each_line 4.272k i/100ms
open + each_line 4.075k i/100ms
open + readlines 4.050k i/100ms
foreach + count 3.569k i/100ms
foreach + inject 2.979k i/100ms
foreach 3.397k i/100ms
wc 26.000 i/100ms
-------------------------------------------------
read + each_line 44.535k (± 2.7%) i/s - 226.416k
open + each_line 42.232k (± 2.7%) i/s - 211.900k
open + readlines 41.585k (± 5.8%) i/s - 210.600k
foreach + count 38.254k (± 1.1%) i/s - 192.726k
foreach + inject 31.478k (± 0.8%) i/s - 157.887k
foreach 35.819k (± 3.2%) i/s - 180.041k
wc 260.809 (± 3.8%) i/s - 1.326k
Comparison:
read + each_line: 44534.7 i/s
open + each_line: 42232.2 i/s - 1.05x slower
open + readlines: 41584.8 i/s - 1.07x slower
foreach + count: 38254.1 i/s - 1.16x slower
foreach: 35818.6 i/s - 1.24x slower
foreach + inject: 31478.2 i/s - 1.41x slower
wc: 260.8 i/s - 170.76x slower
@ilyazub
Copy link

ilyazub commented Sep 11, 2023

An updated benchmark results:

$ bundle exec rails r tmp/html_length_vs_count_vs_size_bench.rb
Warming up --------------------------------------
        lines + size    19.000  i/100ms
      lines + length   130.000  i/100ms
       lines + count   122.000  i/100ms
   each_line + count   127.000  i/100ms
           count($/)   172.000  i/100ms
each_line + take + count
                       159.000  i/100ms
each_line + lazy + take + count
                       155.000  i/100ms
each_line + lazy + take + count
                       145.000  i/100ms
Calculating -------------------------------------
        lines + size      1.205k (±28.3%) i/s -      5.548k in   5.007348s
      lines + length      1.117k (±24.5%) i/s -      4.940k in   5.074537s
       lines + count    999.275  (±43.0%) i/s -      3.904k in   5.142585s
   each_line + count    962.420  (±42.7%) i/s -      3.937k in   5.386932s
           count($/)      1.696k (± 3.4%) i/s -      8.600k in   5.077876s
each_line + take + count
                          1.050k (±42.9%) i/s -      4.134k in   5.084343s
each_line + lazy + take + count
                        918.077  (±50.5%) i/s -      3.565k in   5.057191s
each_line + lazy + take + count
                        941.035  (±49.4%) i/s -      3.770k in   5.265527s

Comparison:
           count($/):     1695.7 i/s
        lines + size:     1205.1 i/s - 1.41x  (± 0.00) slower
      lines + length:     1116.8 i/s - 1.52x  (± 0.00) slower
each_line + take + count:     1050.0 i/s - 1.61x  (± 0.00) slower
       lines + count:      999.3 i/s - 1.70x  (± 0.00) slower
   each_line + count:      962.4 i/s - 1.76x  (± 0.00) slower
each_line + lazy + take + count:      941.0 i/s - 1.80x  (± 0.00) slower

Code:

require "benchmark/ips"

Benchmark.ips do |x|
  x.report("lines + size") { html.lines.size < 50 }
  x.report("lines + length") { html.lines.length < 50 }
  x.report("lines + count") { html.lines.count < 50 }

  x.report("count($/)") { html.count($/) < 50 }

  x.report("each_line + count") { html.each_line.count < 50 }
  x.report("each_line + take + count") { html.each_line.take(50).count < 50 }
  x.report("each_line + lazy + take + count") { html.each_line.lazy.take(50).count < 50 }
  x.report("each_line + lazy + take + count") { !html.each_line.lazy.drop(49).any? }

  x.compare!
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment