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

本文提供了一项针对Ruby 1.8.4/1.8.5(YARV)、Python 2.5在Windows、Cygwin和Ubuntu平台上的性能对比测试。测试涉及代码执行时间和内存占用,并通过调整代码优化了内存使用。结果显示,Python在启用Psyco优化后,性能提升显著,而Ruby的yarv版本在某些场景下表现优于标准实现。
摘要由CSDN通过智能技术生成

Ruby/YARV/Python跨平台性能对比测试报告
作者:Suninny   http://blog.csdn.net/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()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值