Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hiroyuki-sato
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroyuki-sato/4041c5cc561c35d13f8a to your computer and use it in GitHub Desktop.
Save hiroyuki-sato/4041c5cc561c35d13f8a to your computer and use it in GitHub Desktop.
json-streamの速度

結論: とっても遅い

最後の一行が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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment