Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active May 26, 2021 12:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save havenwood/4513627 to your computer and use it in GitHub Desktop.
Save havenwood/4513627 to your computer and use it in GitHub Desktop.
Benchmarking serialization speed of YAML, JSON, Marshal, and MessagePack in Ruby, JRuby, and Rubinius
# ruby-2.5.0
user system total real
YAML 15.112795 0.030577 15.143372 ( 15.190573)
JSON 0.648957 0.001520 0.650477 ( 0.652856)
Marshal 0.474775 0.000922 0.475697 ( 0.477678)
MessagePack 0.326430 0.001763 0.328193 ( 0.330159)
# ruby-2.4.3
user system total real
YAML 20.400000 0.050000 20.450000 ( 20.517600)
JSON 0.920000 0.000000 0.920000 ( 0.927886)
Marshal 0.680000 0.000000 0.680000 ( 0.680348)
MessagePack 0.490000 0.000000 0.490000 ( 0.493976)
# ruby-2.3.6
user system total real
YAML 20.100000 0.620000 20.720000 ( 20.777472)
JSON 1.000000 0.010000 1.010000 ( 1.011932)
Marshal 0.610000 0.010000 0.620000 ( 0.624355)
MessagePack 0.450000 0.010000 0.460000 ( 0.464056)
# jruby-9.1.15.0
user system total real
YAML 16.850000 0.070000 16.920000 ( 16.112922)
JSON 0.620000 0.000000 0.620000 ( 0.621337)
Marshal 0.410000 0.000000 0.410000 ( 0.399778)
MessagePack 0.340000 0.010000 0.350000 ( 0.316007)
# rbx-3.86
user system total real
YAML 74.938858 2.106479 77.045337 ( 75.922954)
JSON 17.461687 0.173907 17.635594 ( 17.563599)
Marshal 41.666180 0.270911 41.937091 ( 41.968574)
MessagePack 5.480164 0.263498 5.743662 ( 5.389367)
require 'benchmark'
require 'yaml'
require 'json'
require 'msgpack'
Benchmark.bmbm do |bm|
this_many = 100_000
this = {aim: true,
nested: {number: 100_000_000,
string: 'my life close twice before...'}}
bm.report 'YAML' do
this_many.times do
YAML.load YAML.dump(this)
end
end
bm.report 'JSON' do
this_many.times do
JSON.parse JSON.generate(this)
end
end
bm.report 'Marshal' do
this_many.times do
Marshal.load Marshal.dump(this)
end
end
bm.report 'MessagePack' do
this_many.times do
MessagePack.unpack MessagePack.pack(this)
end
end
end
@lzap
Copy link

lzap commented May 26, 2021

Oh that's a difference, I wonder if parsing is even worse for YAML. I guess it should be.

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