Skip to content

Instantly share code, notes, and snippets.

@lomereiter
Created October 27, 2012 08:42
Show Gist options
  • Save lomereiter/3963585 to your computer and use it in GitHub Desktop.
Save lomereiter/3963585 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'zlib'
require 'msgpack'
Genotype = Struct.new :location, :ref, :alleles, :prob
class Genotype
def to_msgpack(buf='')
[location, ref, alleles, prob].to_msgpack(buf)
end
end
NUCS = 'AGCT'
def randGenotype
Genotype.new(rand(100_000_000),
NUCS[rand(4)],
(rand(2)+1).times.map{ NUCS[rand(4)] },
rand())
end
list = 20000.times.map { randGenotype() }
def serializeSimple(lst)
lst.map{|g| [g.location, g.ref, g.alleles, g.prob].join "\t"} .join "\n"
end
def deserializeSimple(str)
str.split(/\n/).map{|line| a = line.split(/\t/); Genotype.new(a[0].to_i, a[1], a[2..-2], a[-1].to_f) }
end
def serializeMsgpack(lst)
MessagePack.pack(lst)
end
def deserializeMsgpack(str)
list = MessagePack.unpack str
list.map {|l, r, a, p| Genotype.new l, r, a, p}
end
Benchmark.bmbm do |b|
b.report("serialization | string") { serializeSimple(list) }
b.report("serialization | msgpack") { serializeMsgpack(list) }
b.report("serialization | string + Marshal.dump") { Marshal.dump(serializeSimple(list)) }
b.report("serialization | msgpack + Marshal.dump") { Marshal.dump(serializeMsgpack(list)) }
b.report("serialization | string + Zlib::Deflate + Marshal.dump") {
Marshal.dump(Zlib::Deflate.deflate(serializeSimple(list), 1))
}
b.report("serialization | msgpack + Zlib::Deflate + Marshal.dump") {
Marshal.dump(Zlib::Deflate.deflate(serializeMsgpack(list), 1))
}
str = serializeSimple(list)
msg = serializeMsgpack(list)
str_dump = Marshal.dump str
msg_dump = Marshal.dump msg
compressed_str_dump = Marshal.dump(Zlib::Deflate.deflate(str, 1))
compressed_msg_dump = Marshal.dump(Zlib::Deflate.deflate(msg, 1))
b.report("deserialization | string") { deserializeSimple(str) }
b.report("deserialization | msgpack") { deserializeMsgpack(msg) }
b.report("deserialization | string + Marshal.load") { deserializeSimple(Marshal.load(str_dump)) }
b.report("deserialization | msgpack + Marshal.load") { deserializeMsgpack(Marshal.load(msg_dump)) }
b.report("deserialization | string + Zlib::Inflate + Marshal.load") {
deserializeSimple(Zlib::Inflate.inflate(Marshal.load(compressed_str_dump)))
}
b.report("deserialization | msgpack + Zlib::Inflate + Marshal.load") {
deserializeMsgpack(Zlib::Inflate.inflate(Marshal.load(compressed_msg_dump)))
}
#puts deserializeSimple(str) == list
#puts deserializeMsgpack(msg) == list
puts "messagepack message size: #{msg_dump.length}"
puts "messagepack message size (with Zlib compression): #{compressed_msg_dump.length}"
puts "string message size: #{str_dump.length}"
puts "string message size (with Zlib compression): #{compressed_str_dump.length}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment