Ruby/YARV/Python跨平台性能对比测试报告
作者:Suninny http://blog.youkuaiyun.com/rails
一、基本测试
Ruby代码:
def test
count = Hash.new(0)
File.read('test.txt').split.each {|word| count[word] += 1 }
p count.to_a.sort_by{|x| x[1]}[-30, 30].reverse
end

if __FILE__ == $0
t1 = Time.now
test
puts t2 = Time.now - t1
end
Python代码:
from time import time
from operator import itemgetter

def test():
count = {}
for word in open("test.txt").read().split():
if count.has_key(word):
count[word] = count[word] + 1
else:
count[word] = 1
print sorted(count.iteritems( ), key=itemgetter(1), reverse=True)[0:30]

if __name__ == "__main__":
t1 = time()
test()
print time()-t1
都蛮简洁的。由于Ruby支持带默认值的初始化,少了几行代码。
输出结果为:
[["the", 11345], ["of", 6538], ["to", 5496], ["and", 4611], ["a", 3509], ["in",2940], ["that", 2511], ["was", 2439], ["his", 2346], ["he", 1913], ["as", 1860], ["had", 1363], ["is", 1359], ["it", 1273], ["not", 1231], ["be", 1208], ["for", 1125], ["on", 1075], ["with", 1068], ["this", 1045], ["by", 878], ["The", 851], ["I", 840], ["have", 836], ["but", 806], ["which", 796], ["all", 787], ["their", 749], ["so", 747], ["at", 740]]
Ruby1.8.4@Win32耗时:0.641s
Python2.5@Win32耗时:0.310s;启用Psyco:0.249s
Ruby1.8.5@Cygwin:0.558s
Ruby-yarv@Cygwin:0.453s
Python2.5@Cygwin:0.312s;启用Psyco:0.246s
Ruby1.8.5@Ubuntu: 0.434s
Ruby-yarv@Ubuntu: 0.373s
Python2.5@Ubuntu:0.271s; Psyco: 0.196s
更新@2006.09.17, 19:03
刚发现Python也有类似于Ruby“默认值初始化”的特性,于是test()函数可以改为:
def test():
count = {}
for word in open("test.txt").read().split(): count[word] = 1 + count.get(word, 0)
print sorted(count.iteritems( ), key=itemgetter(1), reverse=True)[0:30]
二、加测内存占用情况
1、将测试文本扩大10倍,变成 9.5M,前面两个程序的时间和内存消耗分别为(括号中为psyco/yarv的分值):
ruby1.8.4@win32: 5.63,97M
ruby1.8.5@cygwin: 4.77s, 93M(3.84s, 95M)
ruby1.8.5@ubuntu: 3.78s, 91M (3.36s, 91M)
py2.5@win32: 2.51s, 65M(1.85s, 72M)
py2.5@cgwin: 2.58s, 80M(1.94s,110M)
py2.5@ubuntu:2.23s, 63M(1.42s, 63M)
2、因为两者用的都是将文件内容一次性读入数组的方法,所以内存占用量都很大,下面对其进行改进:
Ruby代码:
def test
count = Hash.new(0)
for line in open("test.txt")
for word in line.split
count[word] += 1
end
end
p count.to_a.sort_by{|x| x[1]}[-30, 30].reverse
end

if __FILE__ == $0
t1 = Time.now
test
puts t2 = Time.now - t1
end
Python代码:
from time import time
from operator import itemgetter

def test():
count = {}
for line in open("test.txt"):
for word in line.split():
count[word] = 1 + count.get(word, 0)
print sorted(count.iteritems(), key=itemgetter(1), reverse=True)[0:30]

if __name__ == "__main__":
t1 = time()
test()
print time()-t1
输出为:
[('the', 113450), ('of', 65380), ('to', 54960), ('and', 46110), ('a', 35090), ('in', 29400), ('that', 25110), ('was', 24390), ('his', 23460), ('he', 19130), ('as', 18600), ('had', 13630), ('is', 13590), ('it', 12730), ('not', 12310), ('be', 12080), ('for', 11250), ('on', 10750), ('with', 10680), ('this', 10450), ('by', 8780), ('The', 8510), ('I', 8400), ('have', 8360), ('but', 8060), ('which', 7960), ('all', 7870), ('their', 7490), ('so', 7470), ('at', 7400)]
现在两个程序的时间和内存消耗分别为(括号中为psyco/yarv的分值):
ruby1.8.4@win32:7.3s,5M
ruby1.8.5@cygwin: 6.45s, 5M(6.04s, 5M)
ruby1.8.5@ubuntu: 4.25s, 3.2M(4.11s, 3.2M)
py2.5@win32: 2.34s, 3M(1.54s,5M)
py2.5@cygwin: 2.45s, 4M(1.74s, 6M)
py2.5@ubuntu: 2.25s, 1.7M(1.34s,1.8M)
注意:尽管Ruby的内存占用量只有原来的1/20,但速度也明显慢了下来;而Python内存占用一样骤减,速度也随之提升。
更新@2006.09.17, 22:50
这两个程序最后打印结果的那行都还可以稍稍作下改进:
Ruby的可以改为:
p count.to_a.sort_by{|x| -x[1]}[0, 30]
Python的可以改用2.5版最新引入的nlargest()函数(新发现的很棒的东东,只是Ruby目前还未包含类似的函数,Ruby1.9也没看到,只有一个max_by):
print nlargest(30, count.iteritems(), key=itemgetter(1)) #from heapq import nlargest
更新@2006.09.18, 08:25 新增Ubuntu下的分值
Python经过这几年的发展,进步真的很大,不管是在性能还是标准库的扩充方面。而Ruby在这方面却令人有点失望,有时运行速度只及Python的1/3,YARV也形同虚设。值得注意的是两者在Ubuntu下的表现都不错,尤其是Ruby,有显著提升。