小编t_s*_*cho的帖子

执行速度差异的原因是什么?

我写了一个快速的Python脚本来比较两个文件,每个文件包含无序的哈希值,以验证两个文件除了顺序之外是否相同.然后我用Ruby重写了它以用于教育目的.

Python实现需要几秒钟,而Ruby实现需要大约4分钟.

我有一种感觉,这很可能是由于我缺乏Ruby知识,对我做错了什么想法?

环境是Windows XP x64,Python 2.6,Ruby 1.8.6

蟒蛇

f = open('c:\\file1.txt', 'r')

hashes = dict()

for line in f.readlines():
    if not line in hashes:
        hashes[line] = 1
    else:
        hashes[line] += 1


print "Done file 1"

f.close()

f = open('c:\\file2.txt', 'r')

for line in f.readlines():
    if not line in hashes:
        print "Hash not found!"
    else:
        hashes[line] -= 1

f.close()

print "Done file 2"

num_errors = 0

for key in hashes.keys():
    if hashes[key] != 0:
        print "Uneven hash count: %s" % …
Run Code Online (Sandbox Code Playgroud)

ruby python

7
推荐指数
2
解决办法
513
查看次数

标签 统计

python ×1

ruby ×1