Created
December 17, 2011 14:07
-
-
Save passingloop/1490289 to your computer and use it in GitHub Desktop.
yaml vs json
This file contains 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
require 'benchmark' | |
require 'yaml' | |
require 'rubygems' | |
require 'bundler' | |
Bundler.require(:default) | |
require 'active_support' | |
def token | |
[Array.new(24) { rand(256).chr }.join].pack('m*').chop | |
end | |
puts "token length: #{token.size}" | |
data = {} | |
50.times do | |
data[token] = Array.new(3) { token } | |
end | |
yaml = YAML.dump(data) | |
puts "YAML size: #{yaml.size}" | |
json = ActiveSupport::JSON.encode(data) | |
puts "JSON size: #{json.size}" | |
puts "JSON engine: #{ActiveSupport::JSON.engine}" | |
n = 1000 | |
Benchmark.bmbm do |x| | |
GC.disable | |
x.report('YAML.dump') { n.times do; YAML.dump(data); end } | |
x.report('YAML.load') { n.times do; YAML.load(yaml); end} | |
x.report('JSON.encode') { n.times do; ActiveSupport::JSON.encode(data); end } | |
x.report('JSON.decode') { n.times do; ActiveSupport::JSON.decode(json); end } | |
GC.enable | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment