我有一些海量(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
我的任务是将一个文件中的某些条目替换为另一个文件中的某些条目。该列表单独提供。
我编写了一个已经有效的代码。我的策略是只读取并回显第一个文件,直到找到与替换列表匹配的标头。然后在第二个文件中找到相同的标题,回显条目。最后,切换回回显第一个文件。这种方法唯一的问题是它太快了!我查看了直接访问文件,但它们没有固定的记录长度。有没有人有更好的主意?
欢呼帮助, 富
答案 0 :(得分:0)
文件中的标题是否以任何方式排序?如果没有,那么在第二个文件中创建标题的索引文件应该加快第一次查找。我的fortran非常生疏,但如果您可以将第二个文件中的标题排序为索引文件并引用完整条目的位置,那么您应该能够大大加快速度吗?
答案 1 :(得分:0)
我假设您正在读取文件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
应该这样做。