-
-
Save tomlobato/fc02f97f0bc7770972fd5d1e96a4de61 to your computer and use it in GitHub Desktop.
Benchmarking serialization speed of YAML, JSON, Marshal, and MessagePack in MRI
This file contains hidden or 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
# 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) |
This file contains hidden or 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' | |
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