tmp.json 89 万 568 行 286M 大小
require 'benchmark'
require 'json'
require 'yajl'
Benchmark.bm do |x|
filename = "tmp.json"
x.report('yajl') {
open(filename).each do |line|
Yajl.load(line)
end
}
x.report('yajl-stream') {
parser = Yajl::Parser.new
parser.parse(open(filename)) do |hash|
# puts hash
end
}
x.report('json') {
open(filename).each do |line|
JSON.parse(line)
end
}
end
$ rvm use 1.9.3
Using /home/ul/.rvm/gems/ruby-1.9.3-p327
$ ruby json_bench.rb
user system total real
yajl 23.350000 0.090000 23.440000 ( 23.456669)
yajl-stream 18.270000 0.110000 18.380000 ( 18.390657)
json 27.450000 0.110000 27.560000 ( 27.587871)
$ rvm use ruby-head
Using /home/ul/.rvm/gems/ruby-head
$ ruby -v
ruby 2.0.0dev (2012-11-24) [x86_64-linux]
$ ruby json_bench.rb
user system total real
yajl 11.580000 0.100000 11.680000 ( 11.694789)
yajl-stream 8.150000 0.100000 8.250000 ( 8.256228)
json 11.570000 0.140000 11.710000 ( 11.723911)
Ruby 2.0 yajl-stream 胜...=> 286MB / 8.25 sec 约 35 MB/sec