Skip to content

Instantly share code, notes, and snippets.

@junegunn
Created April 17, 2014 16:27
Show Gist options
  • Save junegunn/10996023 to your computer and use it in GitHub Desktop.
Save junegunn/10996023 to your computer and use it in GitHub Desktop.
Ruby serialization
require 'msgpack'
require 'benchmark'
require 'stringio'
require 'json'
hash = (1..100000).reduce({}) { |hsh, e|
hsh[e] = ['-' * 50, '-' * 50]
hsh
}
Benchmark.bm 25 do |x|
x.report('to_s') do
hash.to_s
end
x.report('Marshal.dump') do
Marshal.dump(hash)
end
x.report('JSON.dump') do
JSON.dump(hash)
end
x.report('MessagePack.pack') do
MessagePack.pack(hash)
end
x.report('manual string composition') do
hash.map { |kv| kv.join("\t") }.join($/)
end
end
user system total real
to_s 0.920000 0.060000 0.980000 ( 0.987841)
Marshal.dump 0.760000 0.030000 0.790000 ( 0.796507)
JSON.dump 0.500000 0.050000 0.550000 ( 0.555070)
MessagePack.pack 0.100000 0.020000 0.120000 ( 0.122173)
manual string composition 0.500000 0.040000 0.540000 ( 0.546654)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment