Skip to content

Instantly share code, notes, and snippets.

@Lukom
Created March 28, 2012 21:16
Show Gist options
  • Save Lukom/2230639 to your computer and use it in GitHub Desktop.
Save Lukom/2230639 to your computer and use it in GitHub Desktop.
Ruby serialization/deserialization
=begin
Test various ruby serializers.
*** LOCAL GEMS ***
bert (1.1.2)
bson_ext (1.6.1)
json (1.6.5)
msgpack (0.4.6)
ox (1.5.3)
yajl-ruby (1.1.0)
ruby -v
ruby 1.8.7 (2011-12-28 patchlevel 357) [i686-darwin11.0.0]
=end
# in rails 2.3.3 console:
require 'bson'
require 'bert'
require 'ox'
require 'yajl'
require 'msgpack'
game = {:deck=>"qs|js|10d|6s|6d|10h|7s|6c|7h|kc|jd|ad|ks|8c|10c|8s|9d|qh|qc|kd|jh|jc|as|ac", :players=>{:current=>:human, :players=>[{:cards=>"9c|ah|10s|6h", :player_id=>:human, :ai_brain=>nil, :active=>true}, {:cards=>"7c|kh|qd", :player_id=>:ai4, :ai_brain=>{:level=>4, :ai_memory=>{:can_win=>nil}}, :active=>true}], :attacker=>:ai4, :finished=>[], :defender=>:human}, :bet=>15, :table=>["7d|8d", "8h|9h", "9s"], :win_status=>nil, :trump_suit=>"c", :last_move_type=>:attack, :discard=>"", :active=>true}
game2 = {"last_move_type"=>"attack", "discard"=>"", "deck"=>"qs|js|10d|6s|6d|10h|7s|6c|7h|kc|jd|ad|ks|8c|10c|8s|9d|qh|qc|kd|jh|jc|as|ac", "trump_suit"=>"c", "win_status"=>nil, "active"=>true, "players"=>{"defender"=>"human", "finished"=>[], "attacker"=>"ai4", "players"=>[{"cards"=>"9c|ah|10s|6h", "active"=>true, "ai_brain"=>nil, "player_id"=>"human"}, {"cards"=>"7c|kh|qd", "active"=>true, "ai_brain"=>{"level"=>4, "ai_memory"=>{"can_win"=>nil}}, "player_id"=>"ai4"}], "current"=>"human"}, "bet"=>15, "table"=>["7d|8d", "8h|9h", "9s"]}
n = 5_000
Benchmark.bm do |x|
x.report('marshal') { n.times { Marshal.load(Marshal.dump(game)) } }
x.report('msgpack') { n.times { MessagePack.unpack(MessagePack.pack(game2)) } }
x.report('bson ') { n.times { BSON.deserialize(BSON.serialize(game)) } }
x.report('bert ') { n.times { BERT.decode(BERT.encode(game)) } }
x.report('json ') { n.times { JSON.parse(game2.to_json) } }
x.report('json2 ') { n.times { ActiveSupport::JSON.decode(game2.to_json) } }
x.report('yajl ') { n.times { Yajl::Parser.parse(Yajl::Encoder.encode(game2)) } }
x.report('yaml ') { n.times { YAML.load(YAML.dump(game)) } }
x.report('ox ') { n.times { Ox.load(Ox.dump(game), :mode => :object) } }
end
=begin
Results:
user system total real
marshal 0.390000 0.000000 0.390000 ( 0.393153)
msgpack 0.210000 0.000000 0.210000 ( 0.160727)
bson 2.340000 0.000000 2.340000 ( 2.343516)
bert 5.450000 0.030000 5.480000 ( 5.469525)
json 4.230000 0.010000 4.240000 ( 4.249227)
json2 7.490000 0.020000 7.510000 ( 7.515217)
yajl 0.320000 0.010000 0.330000 ( 0.320377)
yaml 14.880000 0.150000 15.030000 ( 15.025576)
ox 1.850000 0.000000 1.850000 ( 1.858068)
Conclusion:
for binary serialization msgpack gem is interesting, but it doesn`t support complex types (even symbols) so generally marshal is the best for that.
for json - yajl gem is very cool.
for yaml & xml I have tested just one gem for each, so - unknown.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment