Ruby/YARV/Python跨平台性能对比测试报告
作者:Suninny http://blog.csdn.net/rails
问题描述:从一个文件中选出使用频率最多的30个单词
数据来源: http://www.gutenberg.org/dirs/etext03/kpnng10.zip
一、基本测试
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()