Last active
December 12, 2015 04:59
-
-
Save luikore/4718704 to your computer and use it in GitHub Desktop.
marshal is faster and smaller than json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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