Skip to content

Instantly share code, notes, and snippets.

@all4miller
Last active July 11, 2021 00:32
Show Gist options
  • Select an option

  • Save all4miller/8a5b536e04557492e8cf8632153725d1 to your computer and use it in GitHub Desktop.

Select an option

Save all4miller/8a5b536e04557492e8cf8632153725d1 to your computer and use it in GitHub Desktop.

Revisions

  1. all4miller revised this gist Jun 30, 2021. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions csv_bm.rb
    Original 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') do
    x.report("CSV lib & csv << row") do
    CSV.open('one.csv', 'wb') do |csv|
    ary.each { |row| csv << row }
    end
    end
    x.report('file to_csv') do
    x.report("arr map/to_csv rows") do
    IO.write('two.csv', ary.map(&:to_csv).join)
    end
    x.report('file map and join') do
    x.report("arr map/join rows") do
    IO.write('three.csv', ary.map { |row| row.join(',') }.join("\n"))
    end
    x.report("file join") do
    x.report("joined string arr") do
    IO.write('four.csv', str_ary.join("\n"))
    end
    x.compare!
    end
    end
  2. all4miller created this gist Jun 30, 2021.
    29 changes: 29 additions & 0 deletions csv_bm.rb
    Original 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