gpt4 book ai didi

Java:如何从大文本文件中快速提取匹配行?

转载 作者:行者123 更新时间:2023-11-30 03:37:05 25 4
gpt4 key购买 nike

尽管意识到有很多针对我的问题的解决方案一般,我仍然对我的特殊情况下所需的运行时间不满意。

考虑 FASTA 格式的 35G文本文件,如下所示:

>Protein_1 So nice and cute little fellaMTTKKCLQKFHLESLGKLGDSFLKYAISIQLFKSYENHYEGLPSIKKNKIISNAALFKLG YARKILRFIRNEPFDLKVGLIPSDNSQAYNFGKEFLMPSVKMCSRVK*>Protein_2 Fancy incredible description of its functionMADDSKFCFFLVSTFLLLAVVVNVTLAANYVPGDDILLNCGGPDNLPDADGRKWGTDIGS[…] etc.

I need to extract the > lines only.

Using grep '>' proteins.fasta > protein_descriptions.txt to achieve this takes only a couple of minutes.

But using Java 7 this is now already running for over 90 minutes:

public static void main(String[] args) throws Exception {
BufferedReader fastaIn = new BufferedReader(new FileReader(args[0]));
List<String> l = new ArrayList<String>();
String str;
while ((str = fastaIn.readLine()) != null) {
if (str.startsWith(">")) {
l.append(str);
}
}
fastaIn.close();
// …
}

有人知道如何加快 grep 性能吗?

我们将非常感谢您的帮助。干杯!

最佳答案

如果您立即将其写入输出文件而不是在内存中累积对象,它将提高性能(并且无论如何都会更像您使用 grep 所做的那样)。

...
BufferedWriter fastaOut = new BufferedWriter(new FileWriter(args[1]));
...
while ((str = fastaIn.readLine()) != null) {
if (str.startsWith(">")) {
fastaOut.write(str);
fastaOut.newLine();
}
}
...
fastaOut.close();

关于Java:如何从大文本文件中快速提取匹配行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27610012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com