Skip to content

Instantly share code, notes, and snippets.

@paneq
Forked from aishfenton/serializer_benchmarks.rb
Created January 13, 2016 11:06
Show Gist options
  • Select an option

  • Save paneq/2849d929e2c95dc13363 to your computer and use it in GitHub Desktop.

Select an option

Save paneq/2849d929e2c95dc13363 to your computer and use it in GitHub Desktop.

Revisions

  1. aishafenton revised this gist Aug 21, 2010. 1 changed file with 19 additions and 6 deletions.
    25 changes: 19 additions & 6 deletions serializer_benchmarks.rb
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,15 @@
    # sudo gem install bson_ext
    # sudo gem install yajl-ruby
    # sudo gem install json
    # sudo gem install msgpack

    require 'rubygems'
    require 'benchmark'
    require 'yaml'
    require 'bson'
    require 'json'
    require 'yajl'
    require 'msgpack'

    def encode(msg, format)
    case format
    @@ -22,6 +24,8 @@ def encode(msg, format)
    str = Yajl::Encoder.encode(msg)
    when :bson
    str = BSON.serialize(msg).to_s
    when :msgpack
    str = MessagePack.pack(msg)
    end
    str
    end
    @@ -39,6 +43,8 @@ def decode(str, format)
    msg = Yajl::Parser.parse(str)
    when :bson
    msg = BSON.deserialize(str)
    when :msgpack
    msg = MessagePack.unpack(str)
    end
    msg
    end
    @@ -84,13 +90,20 @@ def decode(str, format)
    end
    end

    r.report("MessagePack") do
    SAMPLES.times do
    msg = decode(encode(obj, :msgpack), :msgpack)
    end
    end

    end

    ##
    # Results
    # -------
    # user system total real
    # Marshal 0.100000 0.000000 0.100000 ( 0.098861)
    # JSON (using Yajl) 0.100000 0.000000 0.100000 ( 0.104612)
    # JSON (built-in ruby 1.9.2) 0.250000 0.000000 0.250000 ( 0.253934)
    # BSON 0.270000 0.000000 0.270000 ( 0.260541)
    # YAML 1.120000 0.020000 1.140000 ( 1.137365)
    # Marshal 0.090000 0.000000 0.090000 ( 0.097608)
    # JSON (built-in ruby 1.9.2) 0.250000 0.000000 0.250000 ( 0.261509)
    # JSON (using Yajl) 0.110000 0.020000 0.130000 ( 0.121666)
    # BSON 0.260000 0.000000 0.260000 ( 0.263860)
    # YAML 1.160000 0.020000 1.180000 ( 1.174353)
    # MessagePack 0.030000 0.000000 0.030000 ( 0.030526)
  2. aishafenton revised this gist Jul 18, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions serializer_benchmarks.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    # sudo gem install bson
    # sudo gem install bson_ext
    # sudo gem install yajl-ruby
    # sudo gem install json

    require 'rubygems'
    require 'benchmark'
  3. aishafenton revised this gist Jul 18, 2010. 2 changed files with 95 additions and 44 deletions.
    44 changes: 0 additions & 44 deletions performance_of_yaml_vs_marshal.rb
    Original file line number Diff line number Diff line change
    @@ -1,44 +0,0 @@
    require 'benchmark'
    require 'yaml'

    def encode(msg, format)
    case format
    when :yaml
    str = msg.to_yaml
    when :binary
    str = Marshal.dump(msg)
    end
    str.gsub!("\n", '--\\n')
    str
    end

    def decode(str, format)
    case format
    when :yaml
    YAML.load(str)
    when :binary
    Marshal.load(str)
    end
    str.gsub!('--\\n', "\n")
    end

    SAMPLES = 1000
    obj = {:test => "me"}

    Benchmark.bm do |r|
    r.report("Marshal") do
    SAMPLES.times do
    decode(encode(obj, :binary), :binary)
    end
    end
    r.report("YAML") do
    SAMPLES.times do
    decode(encode(obj, :yaml), :yaml)
    end
    end
    end

    # Results
    # user system total real
    # Marshal 0.020000 0.000000 0.020000 (0.025426)
    # YAML 0.210000 0.010000 0.220000 (0.218788)
    95 changes: 95 additions & 0 deletions serializer_benchmarks.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    # sudo gem install bson
    # sudo gem install bson_ext
    # sudo gem install yajl-ruby

    require 'rubygems'
    require 'benchmark'
    require 'yaml'
    require 'bson'
    require 'json'
    require 'yajl'

    def encode(msg, format)
    case format
    when :yaml
    str = msg.to_yaml
    when :binary
    str = Marshal.dump(msg)
    when :json
    str = JSON.generate(msg)
    when :yajl
    str = Yajl::Encoder.encode(msg)
    when :bson
    str = BSON.serialize(msg).to_s
    end
    str
    end

    def decode(str, format)
    msg = nil
    case format
    when :yaml
    msg = YAML.load(str)
    when :binary
    msg = Marshal.load(str)
    when :json
    msg = JSON.parse(str)
    when :yajl
    msg = Yajl::Parser.parse(str)
    when :bson
    msg = BSON.deserialize(str)
    end
    msg
    end

    SAMPLES = 5_000
    obj = {
    :name => "Fredrick Smith",
    :quantity => 1_000_000,
    :addresses => {
    :address1 => "12 Heather Street, Parnell, Auckland, New Zealand",
    :address2 => "1 Queen Street, CBD, Auckland, New Zealand"
    }
    }

    Benchmark.bmbm do |r|
    r.report("Marshal") do
    SAMPLES.times do
    decode(encode(obj, :binary), :binary)
    end
    end

    r.report("JSON (built-in ruby 1.9.2)") do
    SAMPLES.times do
    decode(encode(obj, :json), :json)
    end
    end

    r.report("JSON (using Yajl)") do
    SAMPLES.times do
    decode(encode(obj, :yajl), :yajl)
    end
    end

    r.report("BSON") do
    SAMPLES.times do
    decode(encode(obj, :bson), :bson)
    end
    end

    r.report("YAML") do
    SAMPLES.times do
    decode(encode(obj, :yaml), :yaml)
    end
    end

    end

    ##
    # Results
    # user system total real
    # Marshal 0.100000 0.000000 0.100000 ( 0.098861)
    # JSON (using Yajl) 0.100000 0.000000 0.100000 ( 0.104612)
    # JSON (built-in ruby 1.9.2) 0.250000 0.000000 0.250000 ( 0.253934)
    # BSON 0.270000 0.000000 0.270000 ( 0.260541)
    # YAML 1.120000 0.020000 1.140000 ( 1.137365)
  4. visfleet created this gist Sep 22, 2009.
    44 changes: 44 additions & 0 deletions performance_of_yaml_vs_marshal.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    require 'benchmark'
    require 'yaml'

    def encode(msg, format)
    case format
    when :yaml
    str = msg.to_yaml
    when :binary
    str = Marshal.dump(msg)
    end
    str.gsub!("\n", '--\\n')
    str
    end

    def decode(str, format)
    case format
    when :yaml
    YAML.load(str)
    when :binary
    Marshal.load(str)
    end
    str.gsub!('--\\n', "\n")
    end

    SAMPLES = 1000
    obj = {:test => "me"}

    Benchmark.bm do |r|
    r.report("Marshal") do
    SAMPLES.times do
    decode(encode(obj, :binary), :binary)
    end
    end
    r.report("YAML") do
    SAMPLES.times do
    decode(encode(obj, :yaml), :yaml)
    end
    end
    end

    # Results
    # user system total real
    # Marshal 0.020000 0.000000 0.020000 (0.025426)
    # YAML 0.210000 0.010000 0.220000 (0.218788)