Ruby Ruby1.9.3 与 Ruby2.0dev 性能比较

juanito · 2012年11月25日 · 最后由 googya 回复于 2012年11月25日 · 4214 次阅读

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

原文

@luikore slide 的地址有问题, www.atdot.net/~ko1/activities/rubyconf2012_ko1_pub.pdf

ps:试了一下,应该是 ruby-china 的地址转换出现了问题,加 http 协议头 就会转义,但结果就有问题

需要 登录 后方可回复, 如果你还没有账号请 注册新账号