Skip to content

Instantly share code, notes, and snippets.

@koshigoe
Created January 3, 2019 00:08
Show Gist options
  • Save koshigoe/b86d33c6dc3d55a5742d6feee88941f9 to your computer and use it in GitHub Desktop.
Save koshigoe/b86d33c6dc3d55a5742d6feee88941f9 to your computer and use it in GitHub Desktop.
Ruby csv 3.0.2
require 'benchmark'
require 'csv'
TEST_FILE = 'test.csv'
unless File.exist?(TEST_FILE)
ROW_SIZE = 1_000_000
ROW = ('a'..'z').map { |c| %Q{"",""#{c}""\n""#{c}""\r""#{c}""\r\n""} }
CSV.open(TEST_FILE, 'wb') do |csv|
ROW_SIZE.times { csv << ROW }
end
end
Benchmark.bm(16) do |x|
x.report('CSV.foreach') do
CSV.foreach(TEST_FILE) { |_| }
end
end
__END__
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
user system total real
CSV.foreach 185.305364 0.243049 185.548413 (185.557201)
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
user system total real
CSV.foreach 149.647574 0.340243 149.987817 (150.005505)
require 'benchmark'
require 'csv'
N = 1_000_000
ROW = ('a'..'z').map { |c| %Q{"",""#{c}""\n""#{c}""\r""#{c}""\r\n""} }
Benchmark.bm(16) do |x|
x.report('CSV.generate_line') do
N.times do
CSV.generate_line(ROW, encoding: 'UTF-8')
end
end
x.report('CSV#string') do
csv = CSV.new('')
N.times do
csv.reopen('')
csv << ROW
csv.string
end
end
end
__END__
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
user system total real
CSV.generate_line104.335042 0.003767 104.338809 (104.323828)
CSV#string 70.865474 0.000000 70.865474 ( 70.862574)
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
user system total real
CSV.generate_line 75.774594 0.004336 75.778930 ( 75.784888)
CSV#string 62.739028 0.000000 62.739028 ( 62.744933)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment