Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aishfenton/480059 to your computer and use it in GitHub Desktop.
Save aishfenton/480059 to your computer and use it in GitHub Desktop.
Performance comparison of different ruby serializer methods
# 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)
@aishfenton
Copy link
Author

Results

Marshal                      0.100000   0.000000   0.100000 (  0.098861)
JSON (using Yajl)            0.100000   0.000000   0.100000 (  0.104612)
JSON (built-in ruby 1.9.2)   0.250000   0.000000   0.250000 (  0.253934)
BSON                         0.270000   0.000000   0.270000 (  0.260541)
YAML                         1.120000   0.020000   1.140000 (  1.137365)

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.

@aishfenton
Copy link
Author

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)

@ms-ati
Copy link

ms-ati commented Feb 10, 2017

Would you consider adding Ox and Oj ?

@ekowcharles
Copy link

ekowcharles commented Jul 13, 2018

# https://github.com/ohler55/oj
# gem install oj

require 'oj'
OJ                           0.001104   0.000001   0.001105 (  0.001104)

Beats MessagePack!

@imm
Copy link

imm commented Dec 19, 2019

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)

@noraj
Copy link

noraj commented Apr 29, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment