Created
May 1, 2014 19:27
-
-
Save jkamenik/9867a246450542e901d8 to your computer and use it in GitHub Desktop.
Performance testing CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'benchmark' | |
require 'csv' | |
file = ARGV.first | |
Benchmark.bm 40, 'total' do |x| | |
t1 = x.report "csv parse" do | |
CSV.parse(File.open(file), headers: true) do |row| | |
# get the 'pr' row | |
row['sa'] | |
end | |
end | |
t2 = x.report 'csv foreach' do | |
CSV.foreach(file, headers: true) do |row| | |
# get the 'pr' row | |
row['sa'] | |
end | |
end | |
t3 = x.report 'csv foreach without headers' do | |
headers = nil | |
CSV.foreach(file) do |row| | |
if headers.nil? | |
headers = row | |
next | |
end | |
# get the pr 'row' | |
row[headers.index('sa')] | |
end | |
end | |
[t1+t2+t3] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output
A 40% increase in speed by not converting the row to a hash.