Created
September 22, 2009 05:55
-
-
Save visfleet/190849 to your computer and use it in GitHub Desktop.
Performance comparison of Ruby's YAML vs Marshal
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' | |
def encode(msg, format) | |
case format | |
when :yaml | |
str = msg.to_yaml | |
when :binary | |
str = Marshal.dump(msg) | |
end | |
str.gsub!("\n", '--\\n') | |
str | |
end | |
def decode(str, format) | |
case format | |
when :yaml | |
YAML.load(str) | |
when :binary | |
Marshal.load(str) | |
end | |
str.gsub!('--\\n', "\n") | |
end | |
SAMPLES = 1000 | |
obj = {:test => "me"} | |
Benchmark.bm do |r| | |
r.report("Marshal") do | |
SAMPLES.times do | |
decode(encode(obj, :binary), :binary) | |
end | |
end | |
r.report("YAML") do | |
SAMPLES.times do | |
decode(encode(obj, :yaml), :yaml) | |
end | |
end | |
end | |
# Results | |
# user system total real | |
# Marshal 0.020000 0.000000 0.020000 (0.025426) | |
# YAML 0.210000 0.010000 0.220000 (0.218788) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment