summaryrefslogtreecommitdiff
path: root/bin/csv-benchmark
blob: dc0bb805a926767e15c6b93820643cc7d4206a86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'

  gem 'benchmark-ips', '~> 2.8'
  gem 'fastcsv', '~> 0.0'
  gem 'fastest-csv'
end

require 'benchmark/ips'
require 'csv'
require 'fastcsv'
require 'fastest-csv'

csv = '"spandx","0.0.0","MIT"'

Benchmark.ips do |x|
  x.report('CSV.parse') { CSV.parse(csv)[0] }
  x.report('csv.split') { csv.split(',', 3) }
  x.report('csv.split-with-slice') { csv.chomp.split(',', 3).slice(1...-1) }
  x.report('csv.regex') { csv.scan(/"(\S+)","*(\d+.\d+.\d+)","(\S+)"/)[0] }
  x.report('csv.fastcsv') { FastCSV.raw_parse(csv) { |y| y } }
  x.report('csv.fastestcsv') { FastestCSV.parse_line(csv) }
  x.compare!
end