Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmarking serialization speed of YAML, JSON, Marshal, and MessagePack in MRI
# 2.4.2p198 on MBP 2017 i7
user system total real
-- YAML size=86 delta_vsz=0 delta_rss=2092
17.730000 0.060000 17.820000 ( 17.851288)
-- JSON size=83 delta_vsz=0 delta_rss=4
0.850000 0.000000 0.870000 ( 0.892370)
-- Marshal size=79 delta_vsz=0 delta_rss=0
0.620000 0.000000 0.640000 ( 0.648228)
-- MessagePack size=63 delta_vsz=128 delta_rss=828
0.380000 0.000000 0.410000 ( 0.406834)
require 'benchmark'
require 'yaml'
require 'json'
require 'msgpack'
def get_mem
`ps ux -p #{$$} |grep -v USER|awk '{print $5, $6}'`
.split(/\s+/)
.map(&:to_i)
end
def _report klass, met_dump, met_load, this, this_many # yes, quick & dirty
mem0 = get_mem
i = 0
ser_size = nil
this_many.times do
ser = klass.send met_dump, this
ser_size = ser.size if (i += 1) == 1
klass.send met_load, ser
end
mem1 = get_mem
puts "size=#{ser_size} delta_vsz=#{mem1[0] - mem0[0]} delta_rss=#{mem1[1] - mem0[1]}"
end
Benchmark.bmbm do |bm|
this_many = 100000
this = {aim: true,
nested: {number: 100_000_000,
string: 'my life close twice before...'}}
bm.report "-- YAML" do
_report YAML, :dump, :load, this, this_many
end
bm.report "-- JSON" do
_report JSON, :generate, :parse, this, this_many
end
bm.report "-- Marshal" do
_report Marshal, :dump, :load, this, this_many
end
bm.report "-- MessagePack" do
_report MessagePack, :pack, :unpack, this, this_many
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.