接上篇: 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
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)
==============================================
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)
==============================================
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有了很大的提高。
on
on
on
on
on
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)
帮你格式化了下,呵呵~
推荐codecolorer这个wordpress plugin
哇,这个看起来相当不错,我那个太慢了,换之
嗯,这个比那个syntaxhighlighter好看~
你系统有没有Monaco这个字体,有的话效果应该最佳~
我用的奏四Monaco啊,热泪盈眶中,刚刚在windows下没看出来,现在看出来了~我也赶快换
python 3.0在做什么?
为什么越搞越慢?
我的试验结果和Sunng的截然相反呀……
# python newlist.py 1000000
221.020936966
# python newlist.py 1000000
220.780134201
# python newlist.py 1000000
220.471858978
# python3 newlist.py 1000000
121.610164642
# python3 newlist.py 1000000
122.308969498
# python3 newlist.py 1000000
123.864889145
不过python3是3.1
难道是这里的第三条?
http://docs.python.org/3.1/whatsnew/3.1.html#optimizations
archlinux,对吧?
arch的python3确实加了那啥,–with-computed-gotos
http://repos.archlinux.org/wsvn/community/python3/trunk/PKGBUILD
不是吧,这个选项对性能影响有那么大?
原来是for loop的bytecode优化
恩,确实有点夸张,我只是看着像
这个goto……
本质的memory allocation速度还是不行阿= =
看来是3.0做功能,3.1做性能
我这里是3.0的,ubuntu啊,跟进慢啊~
最近更新很频繁啊
恩,你可以以此判断我是不是available for pingpang,哈哈