Skip to content

Instantly share code, notes, and snippets.

@luikore
Last active December 12, 2015 04:59
Show Gist options
  • Save luikore/4718704 to your computer and use it in GitHub Desktop.
Save luikore/4718704 to your computer and use it in GitHub Desktop.
marshal is faster and smaller than json
require "json"
require "benchmark"
h = {}
1000.times{|i|h[i]=0}
print "marshaled size: "
marshaled = Marshal.dump(h)
puts marshaled.bytesize
print "jsoned size: "
jsoned = h.to_json
puts jsoned.bytesize
puts 'marshal dump'
puts Benchmark.measure {
1000.times{
Marshal.dump h
}
}
puts 'json dump'
puts Benchmark.measure {
1000.times{
h.to_json
}
}
puts 'marshal load'
puts Benchmark.measure {
1000.times{
Marshal.load marshaled
}
}
puts 'json load'
puts Benchmark.measure {
1000.times{
JSON.load jsoned
}
}
__END__
marshaled size: 5627
jsoned size: 7891
marshal dump
0.200000 0.000000 0.200000 ( 0.200995)
json dump
0.620000 0.000000 0.620000 ( 0.612358)
marshal load
0.300000 0.010000 0.310000 ( 0.320537)
json load
0.830000 0.000000 0.830000 ( 0.827750)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment