Created
August 12, 2011 15:08
-
-
Save glurp/1142245 to your computer and use it in GitHub Desktop.
Serialisation benchmark
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
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) | |
require 'yajl' | |
require 'benchmark' | |
require 'pp' | |
require 'zlib' | |
def fract(n) n==0? 'eeee' : {"l"+n.to_s => fract(n-1) , :i=> (0..n).to_a, "r"+n.to_s => fract(n-1) } ; end | |
class String ; def zip() Zlib::Deflate.deflate(self,3) end end | |
ITER = 1_000 | |
small = [:ok, :answers, [42] * 42] | |
large = ["abc" * 1000] * 100 | |
complex = fract(10) | |
puts "Size of data, and size zipped :" | |
puts("YAJL complex size #{Yajl::Encoder.encode(complex).size} #{Yajl::Encoder.encode(complex).zip.size}") | |
puts("Mars complex size #{Marshal.dump(complex).size} #{Marshal.dump(complex).zip.size}") | |
puts("Ruby complex size #{complex.inspect.size} #{complex.inspect.zip.size}") | |
Benchmark.bm do |bench| | |
bench.report("YAJL small") {(ITER*2).times {Yajl::Parser.parse(Yajl::Encoder.encode(small))}} | |
bench.report("Mars small") {(ITER*2).times {Marshal.load(Marshal.dump(small))}} | |
bench.report("Ruby small") {(ITER*2).times {eval(small.inspect)}} | |
puts | |
bench.report("YAJL large") {ITER.times {Yajl::Parser.parse(Yajl::Encoder.encode(large))}} | |
bench.report("Mars large") {ITER.times {Marshal.load(Marshal.dump(large))}} | |
bench.report("Ruby large") {ITER.times {eval(large.inspect)}} | |
puts | |
iter=ITER/4 | |
bench.report("YAJL complex") {iter.times {Yajl::Parser.parse(Yajl::Encoder.encode(complex))}} | |
bench.report("Mars complex") {iter.times {Marshal.load(Marshal.dump(complex))}} | |
bench.report("Ruby complex") {iter.times {eval(complex.inspect)}} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment