我该怎么做才能使用 Ruby 在一个非常大的 txt 文件中搜索字符串



我遇到了一个问题,我找不到解决它的好方法。

问题描述:

File 1: short_map.txt, contains with over 2millon lines with each line consist of a short url like the one in twitter and its corresponding full web url. 

(例如。http://bit.ly/18sy7Fzhttp://www.london24.com/spurs_star_townsend_deemed_hodgson_joke_a_compliment_1_2903643?utm_source=Daily+News&utm_medium=twitter")

File 2: html_index.txt, contains with about 50k lines with each line stands for a full web url. 

(例如。http://www.redbubble.com/people/tipptoggy/works/10898437-rock-of-cashel")

我想获取html_index.txt文件中每个 Web url 的相应短 url 并将其输出到新的 txt 文件中。

我的做法是阅读html_index.txt的每一行,然后将其与short_map.txt中的每一行进行比较,这样我就可以得到我想要的一切。问题是:太慢了!

谁能帮我用更快的算法来做到这一点?

问题已解决:使用哈希表即可,请参考第一个答案!谢谢!

将short_map.txt文件内容读入哈希,其中键中是长 url,相应的短 url 是其值。当您想要检索短网址时,只需进行哈希查找,这非常快。

相关内容

最新更新