慢速成对比较

时间:2017-06-09 18:13:37

标签: performance list set set-comprehension

我有一个打开两个文件的代码,将它们的内容保存到集合(set1和set2),并将这些集合之间的成对比较结果保存到输出文件中。这两个文件都很大(每个超过100K行),这段代码需要很长时间才能输出(超过10小时)。

有没有办法优化其性能?

def matches2smiles():
    with open('file1.txt') as f:
    set1 = {a.rstrip('\n') for a in f}

    with open('file2.txt') as g:
        set2 = {b.replace('\n', '') for b in g}

    with open('output.txt', 'w') as h: 
        r = [                                                                    
            h.write(b + '\n')
            for a in set1
            for b in set2
            if a in b
            ]

matches2smiles()

1 个答案:

答案 0 :(得分:0)

您的代码首先是伪造的,应该是:

import Cocoa;
import WebKit;
class ViewController: NSViewController {
    @IBOutlet weak var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let url=URL(string: "http://safetec-cam.biz/images/webcam_extern/bad-nauheim_bahnhof_west.jpg");
        webView.load(URLRequest(url: url!));
    }
}

无论如何,使用 r = [ h.write(a + '\n') for a in set1 if a in set2 ] - 它可能会更快更清晰。