List performance: ruby 1.8 and 1.9 (Part 2)

接上篇: List performance: ruby 1.8 and 1.9 (Part 1)

有了之前的准备和热身,真正跑起步来似乎轻松不少,先贴上我们用来benchmark的code:

require 'benchmark'

n = ARGV.empty? ? 10 ** 6 : 10 ** ARGV[0].to_i
puts "n=#{n}"
Benchmark.bmbm do |x|
  x.report do
    list = []
    n.times do
      list << 0
    end
  end
end

之前用来benchmark loop performance的code结构上基本没有区别,这里不再赘述,具体的解释亦可参照前文。

下面分别是ruby 1.8和ruby 1.9的一次完整运行( n=10~10^8 )的数据结果(也是我唯一运行的一次…)

ruby 1.8:

n=10
Rehearsal ------------------------------------
   0.000000   0.000000   0.000000 (  0.000014)
--------------------------- total: 0.000000sec

       user     system      total        real
   0.000000   0.000000   0.000000 (  0.000012)
==============================================

n=100
Rehearsal ------------------------------------
   0.000000   0.000000   0.000000 (  0.000118)
--------------------------- total: 0.000000sec

       user     system      total        real
   0.000000   0.000000   0.000000 (  0.000061)
==============================================

n=1000
Rehearsal ------------------------------------
   0.000000   0.000000   0.000000 (  0.000375)
--------------------------- total: 0.000000sec

       user     system      total        real
   0.000000   0.000000   0.000000 (  0.000314)
==============================================

n=10000
Rehearsal ------------------------------------
   0.000000   0.000000   0.000000 (  0.002900)
--------------------------- total: 0.000000sec

       user     system      total        real
   0.010000   0.000000   0.010000 (  0.002790)
==============================================

n=100000
Rehearsal ------------------------------------
   0.030000   0.000000   0.030000 (  0.027712)
--------------------------- total: 0.030000sec

       user     system      total        real
   0.020000   0.000000   0.020000 (  0.027713)
==============================================

n=1000000
Rehearsal ------------------------------------
   0.280000   0.010000   0.290000 (  0.323298)
--------------------------- total: 0.290000sec

       user     system      total        real
   0.280000   0.010000   0.290000 (  0.283621)
==============================================

n=10000000
Rehearsal ------------------------------------
   2.740000   0.050000   2.790000 (  2.809917)
--------------------------- total: 2.790000sec

       user     system      total        real
   2.760000   0.040000   2.800000 (  2.810491)
==============================================

n=100000000
Rehearsal ------------------------------------
  27.410000   0.490000  27.900000 ( 28.164103)
-------------------------- total: 27.900000sec

       user     system      total        real
  27.620000   0.480000  28.100000 ( 28.230073)
==============================================

ruby 1.9:

n=10
Rehearsal ------------------------------------
   0.000000   0.000000   0.000000 (  0.000068)
--------------------------- total: 0.000000sec

       user     system      total        real
   0.000000   0.000000   0.000000 (  0.000012)
==============================================

n=100
Rehearsal ------------------------------------
   0.000000   0.000000   0.000000 (  0.000038)
--------------------------- total: 0.000000sec

       user     system      total        real
   0.000000   0.000000   0.000000 (  0.000032)
==============================================

n=1000
Rehearsal ------------------------------------
   0.000000   0.000000   0.000000 (  0.000297)
--------------------------- total: 0.000000sec

       user     system      total        real
   0.000000   0.000000   0.000000 (  0.000209)
==============================================

n=10000
Rehearsal ------------------------------------
   0.000000   0.000000   0.000000 (  0.002164)
--------------------------- total: 0.000000sec

       user     system      total        real
   0.000000   0.000000   0.000000 (  0.002025)
==============================================

n=100000
Rehearsal ------------------------------------
   0.020000   0.000000   0.020000 (  0.020366)
--------------------------- total: 0.020000sec

       user     system      total        real
   0.020000   0.000000   0.020000 (  0.020873)
==============================================

n=1000000
Rehearsal ------------------------------------
   0.190000   0.010000   0.200000 (  0.208302)
--------------------------- total: 0.200000sec

       user     system      total        real
   0.190000   0.000000   0.190000 (  0.208822)
==============================================

n=10000000
Rehearsal ------------------------------------
   1.920000   0.050000   1.970000 (  2.050276)
--------------------------- total: 1.970000sec

       user     system      total        real
   1.920000   0.050000   1.970000 (  2.054335)
==============================================

n=100000000
Rehearsal ------------------------------------
  19.220000   0.460000  19.680000 ( 20.466960)
-------------------------- total: 19.680000sec

       user     system      total        real
  19.220000   0.460000  19.680000 ( 20.546417)
==============================================

就这次运行的结果而言,ruby 1.9的performance平均都有25%~35%的提升,加之Part 1测试过的循环性能的倒退,则更可以确定list append操作的performance确实在1.9里面较1.8有了很大的提高。

历史上的今天:

Related posts:

17 Comments on "List performance: ruby 1.8 and 1.9 (Part 2)"

  1. Sunng China Mozilla Firefox Ubuntu Linux says:
    sun@sun-laptop:~/work$ python pylist.py
    0.269968986511
    sun@sun-laptop:~/work$ python pylist.py
    0.263886213303
    sun@sun-laptop:~/work$ python pylist.py
    0.261169910431
    sun@sun-laptop:~/work$ python3.0 pylist.py
    0.355209112167
    sun@sun-laptop:~/work$ python3.0 pylist.py
    0.34161400795
    sun@sun-laptop:~/work$ python3.0 pylist.py
    0.336540937424
    sun@sun-laptop:~/work$ cat pylist.py
    import time

    loop = 1000000
    l = []
    t1 = time.time()
    for i in range(loop): l.append(i)
    t2 = time.time()
    print(t2-t1)
  2. Jay United States Google Chrome Windows says:

    最近更新很频繁啊

Got something to say? Go for it!

使用新浪微博登陆