《Ruby/YARV/Python跨平台性能对比测试报告》(附单词频率统计实例)

2023-09-25 7 0

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()
  
代码编程
赞赏

相关文章

最长公共子序列问题LCS Longest Common Subsequence
在使用Eclipse Debug JDK时,看不到变量值的解决办法
实例化bean
飞行器系统辨识
什么是旋翼变体技术
我国航天发展史上的里程碑