Last active
July 11, 2021 00:32
-
-
Save all4miller/8a5b536e04557492e8cf8632153725d1 to your computer and use it in GitHub Desktop.
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
| # 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment