forked from brianmario/yajl-ruby
/
encode_json_and_yaml.rb
47 lines (43 loc) · 978 Bytes
/
encode_json_and_yaml.rb
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# encoding: UTF-8
require 'rubygems'
require 'benchmark'
require 'yajl_ext'
require 'json'
require 'yaml'
# JSON Section
filename = 'benchmark/subjects/contacts.json'
json = File.new(filename, 'r')
hash = Yajl::Parser.new.parse(json)
json.close
times = ARGV[0] ? ARGV[0].to_i : 1
puts "Starting benchmark encoding #{filename} into JSON #{times} times\n\n"
Benchmark.bm { |x|
encoder = Yajl::Encoder.new
x.report {
puts "Yajl::Encoder#encode"
times.times {
encoder.encode(hash, StringIO.new)
}
}
x.report {
puts "JSON's #to_json"
times.times {
JSON.generate(hash)
}
}
}
# YAML Section
filename = 'benchmark/subjects/contacts.yml'
yml = File.new(filename, 'r')
data = YAML.load_stream(yml)
yml.close
times = ARGV[0] ? ARGV[0].to_i : 1
puts "Starting benchmark encoding #{filename} into YAML #{times} times\n\n"
Benchmark.bm { |x|
x.report {
puts "YAML.dump"
times.times {
YAML.dump(data, StringIO.new)
}
}
}