Skip to content

Instantly share code, notes, and snippets.

@all4miller
Last active July 11, 2021 00:32
Show Gist options
  • Save all4miller/8a5b536e04557492e8cf8632153725d1 to your computer and use it in GitHub Desktop.
Save all4miller/8a5b536e04557492e8cf8632153725d1 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'benchmark'
require 'benchmark/ips'
require 'benchmark/memory'
require 'csv'
N_NUMBER_OF = 100_000
ary = Array.new(N_NUMBER_OF) { ['one', 'two', 'three', 'four', 'five', 'six'] }
str_ary = Array.new(N_NUMBER_OF) { 'one,two,three,four,five,six' }
Benchmark.ips do |x|
x.report("CSV lib & csv << row") do
CSV.open('one.csv', 'wb') do |csv|
ary.each { |row| csv << row }
end
end
x.report("arr map/to_csv rows") do
IO.write('two.csv', ary.map(&:to_csv).join)
end
x.report("arr map/join rows") do
IO.write('three.csv', ary.map { |row| row.join(',') }.join("\n"))
end
x.report("joined string arr") do
IO.write('four.csv', str_ary.join("\n"))
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment