Forked from visfleet/performance_of_yaml_vs_marshal.rb
Created
July 18, 2010 02:32
-
-
Save aishfenton/480059 to your computer and use it in GitHub Desktop.
Performance comparison of different ruby serializer methods
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
# sudo gem install bson | |
# sudo gem install bson_ext | |
# sudo gem install yajl-ruby | |
# sudo gem install json | |
# sudo gem install msgpack | |
require 'rubygems' | |
require 'benchmark' | |
require 'yaml' | |
require 'bson' | |
require 'json' | |
require 'yajl' | |
require 'msgpack' | |
def encode(msg, format) | |
case format | |
when :yaml | |
str = msg.to_yaml | |
when :binary | |
str = Marshal.dump(msg) | |
when :json | |
str = JSON.generate(msg) | |
when :yajl | |
str = Yajl::Encoder.encode(msg) | |
when :bson | |
str = BSON.serialize(msg).to_s | |
when :msgpack | |
str = MessagePack.pack(msg) | |
end | |
str | |
end | |
def decode(str, format) | |
msg = nil | |
case format | |
when :yaml | |
msg = YAML.load(str) | |
when :binary | |
msg = Marshal.load(str) | |
when :json | |
msg = JSON.parse(str) | |
when :yajl | |
msg = Yajl::Parser.parse(str) | |
when :bson | |
msg = BSON.deserialize(str) | |
when :msgpack | |
msg = MessagePack.unpack(str) | |
end | |
msg | |
end | |
SAMPLES = 5_000 | |
obj = { | |
:name => "Fredrick Smith", | |
:quantity => 1_000_000, | |
:addresses => { | |
:address1 => "12 Heather Street, Parnell, Auckland, New Zealand", | |
:address2 => "1 Queen Street, CBD, Auckland, New Zealand" | |
} | |
} | |
Benchmark.bmbm do |r| | |
r.report("Marshal") do | |
SAMPLES.times do | |
decode(encode(obj, :binary), :binary) | |
end | |
end | |
r.report("JSON (built-in ruby 1.9.2)") do | |
SAMPLES.times do | |
decode(encode(obj, :json), :json) | |
end | |
end | |
r.report("JSON (using Yajl)") do | |
SAMPLES.times do | |
decode(encode(obj, :yajl), :yajl) | |
end | |
end | |
r.report("BSON") do | |
SAMPLES.times do | |
decode(encode(obj, :bson), :bson) | |
end | |
end | |
r.report("YAML") do | |
SAMPLES.times do | |
decode(encode(obj, :yaml), :yaml) | |
end | |
end | |
r.report("MessagePack") do | |
SAMPLES.times do | |
msg = decode(encode(obj, :msgpack), :msgpack) | |
end | |
end | |
end | |
# Results | |
# ------- | |
# user system total real | |
# Marshal 0.090000 0.000000 0.090000 ( 0.097608) | |
# JSON (built-in ruby 1.9.2) 0.250000 0.000000 0.250000 ( 0.261509) | |
# JSON (using Yajl) 0.110000 0.020000 0.130000 ( 0.121666) | |
# BSON 0.260000 0.000000 0.260000 ( 0.263860) | |
# YAML 1.160000 0.020000 1.180000 ( 1.174353) | |
# MessagePack 0.030000 0.000000 0.030000 ( 0.030526) |
Added another contender 'msgpack' (http://msgpack.org/). Woa, this one is fast, look at the numbers compared to Ruby Marshal:
Marshal 0.090000 0.000000 0.090000 ( 0.097608)
MessagePack 0.030000 0.000000 0.030000 ( 0.030526)
Would you consider adding Ox and Oj ?
# https://github.com/ohler55/oj
# gem install oj
require 'oj'
OJ 0.001104 0.000001 0.001105 ( 0.001104)
Beats MessagePack!
OJ 0.001104 0.000001 0.001105 ( 0.001104)Beats MessagePack!
Really?
user system total real
Marshal 0.035421 0.000000 0.035421 ( 0.035740)
JSON (built-in ruby 1.9.2) 0.045582 0.000000 0.045582 ( 0.045980)
JSON (using Yajl) 0.048620 0.000051 0.048671 ( 0.049481)
YAML 1.040434 0.000050 1.040484 ( 1.048812)
MessagePack 0.020047 0.000001 0.020048 ( 0.020123)
Oj 0.024167 0.000017 0.024184 ( 0.024506)
Better benchmark depending on data type: https://www.koffeinfrei.org/2018/03/21/ruby-serialization-benchmark/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results
Yajl is almost as fast as Ruby's native Marshal -- how does it do that? On the other hand, BSON is a little disappointing in terms of performance. I would have thought that binary JSON would be faster, I guess not. And finally YAML dog slow.