Skip to content

Instantly share code, notes, and snippets.

@b1nary
Created June 27, 2012 13:14
Show Gist options
  • Save b1nary/3003994 to your computer and use it in GitHub Desktop.
Save b1nary/3003994 to your computer and use it in GitHub Desktop.
Ruby file serialation Benchmark
1. YAML: Default Ruby implementation
2. JSON: 'json' gem
3. XML: 'xml-simple' gem
3. Mar.: Marshal (internal)
Test 1.: Data Hash
user system total real
YAML: 2.170000 0.040000 2.210000 ( 2.226518)
JSON: 0.030000 0.000000 0.030000 ( 0.037047)
XML: 29.700000 0.190000 29.890000 ( 31.729220)
Mar.: 0.030000 0.000000 0.030000 ( 0.029675)
Test 2.: Nested Data
user system total real
YAML: 2.520000 0.010000 2.530000 ( 2.547487)
JSON: 0.070000 0.000000 0.070000 ( 0.074080)
XML: 26.220000 0.170000 26.390000 ( 27.841684)
Mar.: 0.430000 0.720000 1.150000 ( 1.862326)
Test 3.: Deep Nested
user system total real
YAML: 1.990000 0.000000 1.990000 ( 2.631946)
JSON: 0.020000 0.000000 0.020000 ( 0.025115)
XML: 27.440000 0.270000 27.710000 ( 30.519407)
Mar.: 0.410000 0.580000 0.990000 ( 1.327428)
require 'benchmark'
require 'rubygems'
require 'xmlsimple'
require 'yaml'
require 'json'
puts "1. YAML: Default Ruby implementation"
puts "2. JSON: 'json' gem"
puts "3. XML: 'xml-simple' gem"
puts "3. Mar.: Marshal (internal)"
puts ""
@data = { :smallchars => ('a'...'z').to_a, :bigchars => ('A'...'Z').to_a, :numbers => (1...100000).to_a }
@nested_data = { :data => { :smallchars => ('a'...'z').to_a, :bigchars => ('A'...'Z').to_a, :numbers => (1...100000).to_a } }
@deep_nested = { :data => { :data => { :data => { :data => { :data => (1...100000).to_a } } } } }
puts ""
puts "Test 1.: Data Hash"
Benchmark.bm do |x|
x.report { print "YAML: "; YAML.load(@data.to_yaml) }
x.report { print "JSON: "; JSON.load(@data.to_json) }
x.report { print "XML: "; XmlSimple.xml_in(XmlSimple.xml_out(@data)) }
x.report { print "Mar.: "; Marshal.load(Marshal.dump(@data)) }
end
puts ""
puts "Test 2.: Nested Data"
Benchmark.bm do |x|
x.report { print "YAML: "; YAML.load(@nested_data.to_yaml) }
x.report { print "JSON: "; JSON.load(@nested_data.to_json) }
x.report { print "XML: "; XmlSimple.xml_in(XmlSimple.xml_out(@nested_data)) }
x.report { print "Mar.: "; Marshal.load(Marshal.dump(@nested_data)) }
end
puts ""
puts "Test 3.: Deep Nested"
Benchmark.bm do |x|
x.report { print "YAML: "; YAML.load(@deep_nested.to_yaml) }
x.report { print "JSON: "; JSON.load(@deep_nested.to_json) }
x.report { print "XML: "; XmlSimple.xml_in(XmlSimple.xml_out(@deep_nested)) }
x.report { print "Mar.: "; Marshal.load(Marshal.dump(@deep_nested)) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment