Skip to content

Instantly share code, notes, and snippets.

@jrunning
Created May 27, 2016 00:23
Show Gist options
  • Save jrunning/229f8c2348fee4ba1d88d0dffa58edb7 to your computer and use it in GitHub Desktop.
Save jrunning/229f8c2348fee4ba1d88d0dffa58edb7 to your computer and use it in GitHub Desktop.
require "benchmark/ips"
require "csv"
# Data: Fielding.csv from Lahman Baseball Database, sorted
# http://seanlahman.com/baseball-archive/statistics/
FILENAME = File.expand_path("Fielding-sorted.csv", __dir__)
MATCH_FIELD = "kruegot01" # Record in the middle of the file
NUM_ROWS = 5 # Grab this many rows (for lack of anything better to do)
def method_a
rows = []
CSV.foreach(FILENAME, headers: true) do |row|
next unless row["playerID"] == MATCH_FIELD
rows << row
return rows if rows.size == NUM_ROWS
end
end
def method_b
file = File.open(FILENAME)
headers = CSV.parse_line(file.gets)
match = "#{MATCH_FIELD},"
while line = file.gets
break if line.start_with?(match)
end
# back up to the beginning of the line
file.seek(-line.size, IO::SEEK_CUR)
CSV.new(file, headers: headers).take(NUM_ROWS)
ensure
file.close
end
Benchmark.ips do |x|
x.report("method_a") { method_a }
x.report("method_b") { method_b }
x.compare!
end
Warming up --------------------------------------
method_a 1.000 i/100ms
method_b 1.000 i/100ms
Calculating -------------------------------------
method_a 0.100 (± 0.0%) i/s - 1.000 in 9.987337s
method_b 6.575 (±15.2%) i/s - 33.000
Comparison:
method_b: 6.6 i/s
method_a: 0.1 i/s - 65.67x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment