tokuhirom's blog

Ruby の deserializer の速度比較

ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]

require 'msgpack'
require 'benchmark'
require 'json'

# https://github.com/fluent/fluentd/blob/master/lib/fluent/plugin/parser_ltsv.rb
class LTSV
  def self.parse(text)
    r = {}
    text.split("\t").each do |pair|
      key, value = pair.split(":", 2)
      r[key] = value
    end
    r
  end

  def self.generate(data)
    data.map {|k,v| k+":"+v }.join("\t")
  end
end

src = Hash[(1..1000).map{|i| i.to_s}.each_slice(2).to_a]

msgpk = src.to_msgpack
json = JSON.generate(src)
ltsv = LTSV.generate(src)

iterations = 100_000

puts "msgpk=#{msgpk.length} bytes"
puts "json=#{json.length} bytes"
puts "ltsv=#{ltsv.length} bytes"
puts ""

Benchmark.bm(10) do |x|
  x.report('msgpack') do
    iterations.times { MessagePack.unpack(msgpk) }
  end
  x.report('json') do
    iterations.times { JSON.parse(json) }
  end
  x.report('ltsv') do
    iterations.times { LTSV.parse(ltsv) }
  end
end
msgpk=3896 bytes
json=5894 bytes
ltsv=3892 bytes

                 user     system      total        real
msgpack     11.390000   0.050000  11.440000 ( 11.509034)
json        31.690000   0.140000  31.830000 ( 32.131092)
ltsv        36.870000   0.270000  37.140000 ( 37.986320)

そんな感じで。

Created: 2018-01-31 19:16:43 +0900
Updated: 2018-01-31 19:16:43 +0900