Skip to content

Instantly share code, notes, and snippets.

@thejspr
Created February 25, 2013 15:00
Show Gist options
  • Save thejspr/5030366 to your computer and use it in GitHub Desktop.
Save thejspr/5030366 to your computer and use it in GitHub Desktop.
JSON vs. Msgpack benchmark
#!/usr/bin/env ruby
# JRuby
# gem install msgpack-jruby
# MRI
# gem install msgpack
require 'benchmark'
COUNT = ARGV[0].to_i || 10
hash = {
wat: 'is this ' * 100,
nested: [{ element: 'x'*rand(10), wat: { is: 'this' } }]*100
}
Benchmark.bm do |x|
x.report('msgpack') do
require 'msgpack'
COUNT.times do
pack = MessagePack.pack(hash)
MessagePack.unpack(pack)
end
end
x.report('json ') do
require 'json'
COUNT.times do
json = JSON(hash)
JSON.parse(json)
end
end
end
@thejspr
Copy link
Author

thejspr commented Feb 25, 2013

~/Code ❯❯❯ ./msgpack_json_benchmark.rb 10
             user     system      total        real
msgpack  0.860000   0.050000   0.910000 (  0.468000)
json     0.270000   0.010000   0.280000 (  0.144000)

/Code ❯❯❯ ./msgpack_json_benchmark.rb 10000
             user     system      total        real
msgpack  4.230000   0.110000   4.340000 (  3.668000)
json    10.380000   0.060000  10.440000 ( 10.120000)

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