Skip to content

Instantly share code, notes, and snippets.

@pauldix
Created March 31, 2010 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pauldix/350956 to your computer and use it in GitHub Desktop.
Save pauldix/350956 to your computer and use it in GitHub Desktop.
# little test script to verify the speed of msgpack vs. json
require 'rubygems'
require 'yajl'
require 'msgpack'
require 'benchmark'
array = (1..20).map {|i| rand(i)}
map = (1..20).inject({}) {|r, o| r[rand(o).to_s] = rand(o); r}
string = (1..512).map {|i| ((rand(i) % 94) + 32).chr.to_s}.join('')
puts array.inspect
puts map.inspect
puts string.inspect
iterations = 200_000
Benchmark.bm do |bench|
bench.report("msgpack") do
iterations.times do
msg = array.to_msgpack
MessagePack.unpack(msg)
msg = map.to_msgpack
MessagePack.unpack(msg)
msg = string.to_msgpack
MessagePack.unpack(msg)
end
end
bench.report("yajl-ruby") do
iterations.times do
json = Yajl::Encoder.encode(array)
Yajl::Parser.parse(json)
json = Yajl::Encoder.encode(map)
Yajl::Parser.parse(json)
json = Yajl::Encoder.encode(string)
Yajl::Parser.parse(json)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment