Skip to content

Instantly share code, notes, and snippets.

@jerhinesmith
Created July 27, 2011 00:31
Show Gist options
  • Save jerhinesmith/1108427 to your computer and use it in GitHub Desktop.
Save jerhinesmith/1108427 to your computer and use it in GitHub Desktop.
yaml/json serialization
require 'benchmark'
require 'json'
require 'yaml'
iterations = 10_000
message = {:to => 'Justin', :from => 'Andres', :subject => 'Foosball Game', :body => "Hey, sorry that I left without at least asking anyone if they wanted to play a quick game of foosball. That was really dumb. Next time I won't be dumb about that."}
message_yaml = YAML::dump(message)
message_json = message.to_json
Benchmark.bm(10) do |x|
x.report("YAML::dump") { (1..iterations).each{|i| YAML::dump(message)} }
x.report("to_json") { (1..iterations).each{|i| message.to_json} }
x.report("YAML::load") { (1..iterations).each{|i| YAML::load(message_yaml)} }
x.report("JSON.parse") { (1..iterations).each{|i| JSON.parse(message_json)} }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment