結論: とっても遅い
最後の一行がjson-streamの結果です。
参考: Yajlって速いの?
Rehearsal ------------------------------------------------------
encode with yajl 0.090000 0.010000 0.100000 ( 0.101176)
encode with yajl 2 0.110000 0.000000 0.110000 ( 0.109449)
encode with json 0.090000 0.000000 0.090000 ( 0.091249)
--------------------------------------------- total: 0.300000sec
user system total real
encode with yajl 0.080000 0.000000 0.080000 ( 0.087069)
encode with yajl 2 0.090000 0.000000 0.090000 ( 0.095520)
encode with json 0.080000 0.000000 0.080000 ( 0.082816)
decode
Rehearsal -----------------------------------------------------------
decode with yajl 0.120000 0.000000 0.120000 ( 0.122609)
decode with yajl 2 0.130000 0.010000 0.140000 ( 0.134408)
decode with json 0.130000 0.000000 0.130000 ( 0.134953)
decode with json 2 0.130000 0.000000 0.130000 ( 0.124944)
decode with json-stream 2.980000 0.010000 2.990000 ( 2.994720)
-------------------------------------------------- total: 3.510000sec
user system total real
decode with yajl 0.120000 0.000000 0.120000 ( 0.116115)
decode with yajl 2 0.120000 0.000000 0.120000 ( 0.118605)
decode with json 0.100000 0.000000 0.100000 ( 0.108177)
decode with json 2 0.100000 0.000000 0.100000 ( 0.102321)
decode with json-stream 2.930000 0.000000 2.930000 ( 2.941352)検証スクリプト
require "benchmark"
require "yajl"
require "json"
require 'json/stream'
#BENCH_TIMES = 100000
BENCH_TIMES = 10_000
hash = { s:"string", ss:["strings"] * 3, n:10, ns:(0..100).to_a.sample(20) }
json = hash.to_json
Benchmark.bmbm do |bm|
bm.report("encode with yajl") do
BENCH_TIMES.times do
encoder = Yajl::Encoder.new
encoder.encode(hash)
end
end
bm.report("encode with yajl 2") do
BENCH_TIMES.times do
Yajl::Encoder.encode(hash)
end
end
bm.report("encode with json") do
BENCH_TIMES.times do
hash.to_json
end
end
end
puts "\ndecode"
Benchmark.bmbm do |bm|
bm.report("decode with yajl") do
BENCH_TIMES.times do
decoder = Yajl::Parser.new
decoder.parse(json)
end
end
bm.report("decode with yajl 2") do
BENCH_TIMES.times do
Yajl::Parser.parse(json)
end
end
bm.report("decode with json") do
BENCH_TIMES.times do
parser = JSON::Parser.new(json)
parser.parse
end
end
bm.report("decode with json 2") do
BENCH_TIMES.times do
JSON.parse(json)
end
end
bm.report("decode with json-stream") do
BENCH_TIMES.times do
JSON::Stream::Parser.parse(json)
end
end
end