public

Performance comparison of different ruby serializer methods

  • Download Gist
serializer_benchmarks.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
# 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)

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.

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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.