Last active
July 11, 2021 00:32
-
-
Save all4miller/8a5b536e04557492e8cf8632153725d1 to your computer and use it in GitHub Desktop.
Revisions
-
all4miller revised this gist
Jun 30, 2021 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -11,19 +11,19 @@ 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 -
all4miller created this gist
Jun 30, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ # 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') do CSV.open('one.csv', 'wb') do |csv| ary.each { |row| csv << row } end end x.report('file to_csv') do IO.write('two.csv', ary.map(&:to_csv).join) end x.report('file map and join') do IO.write('three.csv', ary.map { |row| row.join(',') }.join("\n")) end x.report("file join") do IO.write('four.csv', str_ary.join("\n")) end x.compare! end