我有一些大量(460万行)的数据文件,我正试图用fortran编辑这些文件。基本上,整个文件都是一系列的标题,后面跟着一个数字表格。如下所示:
P he4 blah 99 ggg
1.0e+01 2.0e+01 2.0e+01
2.0e+01 5.0e+01 2.0e+01
。
。
3.2e+-1 2.0e+01 1.0e+00
P he3 blafoo 99 ggg
1.1e+00 2.3e+01 2.0e+01
我的任务是将一个文件中的某些条目替换为另一个文件中的条目。列表是单独提供的。
我已经写了一个已经可以工作的代码。我的策略是只读取并回显第一个文件,直到我找到与替换列表匹配的头文件。然后在第二个文件中找到相同的头文件,回显条目。最后,切换回回第一个文件。这种方法唯一的问题是它太慢了!我查看了文件的直接访问,但它们没有固定的记录长度。有谁有更好的主意吗?
为你的帮助干杯,瑞奇
发布于 2010-10-06 22:25:29
文件中的头文件是否以某种方式排序?如果没有,那么在第二个文件中创建头文件的索引文件应该会加快第一次查找的速度。我的fortran非常生疏,但是如果您可以将第二个文件中的头文件排序到索引文件中,并引用完整条目的位置,您应该能够显著加快速度?
发布于 2010-10-07 00:22:39
我假设您正在读取文件% 1,并将结果写入文件% 3。文件%2包含替换项。
Preprocess file 2, by loading each header, and using a hash algorithm to create
an array with and integer hash representation of each header value in it, and a
pointer/subscript to the values to replace it by.
while there are lines left in file 1
read an original line from file 1
hash the original line to get the hash value.
if the hash value is in the hash array
write the replacement to file 3
else
write the original line to file 3这应该能起到作用。
https://stackoverflow.com/questions/3873430
复制相似问题