gpt4 book ai didi

python - 快速转换大型 (2.1gb+) 二进制文件(纬度/经度/海拔高度至 ECEF)

转载 作者:行者123 更新时间:2023-12-01 06:06:19 24 4
gpt4 key购买 nike

现在,我正在尝试将大量纬度经度高度格式的点的二进制文件转换为基于文本的 ECEF 笛卡尔格式(x、y、z)。现在的问题是这个过程非常非常非常慢。

我有超过 100 GB 的数据需要运行,并且可能会输入更多数据。我希望尽可能快地编写这段代码。

现在我的代码看起来像这样:

import mmap
import sys
import struct
import time

pointSize = 41

def getArguments():
if len(sys.argv) != 2:
print """Not enough arguments.
example:
python tllargbin_reader.py input_filename.tllargbin output_filename
"""
return None
else:
return sys.argv

print getArguments()

def read_tllargbin(filename, outputCallback):
f = open(filename, "r+")
map = mmap.mmap(f.fileno(),0)
t = time.clock()
if (map.size() % pointSize) != 0:
print "File size not aligned."
#return
for i in xrange(0,map.size(),pointSize):
data_list = struct.unpack('=4d9B',map[i:i+pointSize])
writeStr = formatString(data_list)
if i % (41*1000) == 0:
print "%d/%d points processed" % (i,map.size())
print "Time elapsed: %f" % (time.clock() - t)
map.close()


def generate_write_xyz(filename):
f = open(filename, 'w', 128*1024)
def write_xyz(writeStr):
f.write(writeStr)
return write_xyz

def formatString(data_list):
return "%f %f %f" % (data_list[1], data_list[2],data_list[3])
args = getArguments()
if args != None:
read_tllargbin(args[1],generate_write_xyz("out.xyz"))

convertXYZ() 基本上是这里的转换公式: http://en.wikipedia.org/wiki/Geodetic_system

我想知道用一个线程以约 4MB 的 block 读取内容是否会更快,将它们放入有界缓冲区中,使用不同的线程转换为字符串格式,并让最终线程将字符串写回不同硬盘上的文件。不过我可能有点操之过急了……

我现在正在使用 python 进行测试,但如果我可以更快地处理这些文件,我不会反对切换。

任何建议都会很棒。谢谢

编辑:

我再次使用 cProfile 对代码进行了分析,这次拆分了字符串格式和 io.看来我真的被字符串格式杀死了......这是分析器报告

         20010155 function calls in 548.993 CPU seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 548.993 548.993 <string>:1(<module>)
1 0.016 0.016 548.991 548.991 tllargbin_reader.py:1(<module>)
1 24.018 24.018 548.955 548.955 tllargbin_reader.py:20(read_tllargbin)
1 0.000 0.000 0.020 0.020 tllargbin_reader.py:36(generate_write_xyz)
10000068 517.233 0.000 517.233 0.000 tllargbin_reader.py:42(formatString)
2 0.000 0.000 0.000 0.000 tllargbin_reader.py:8(getArguments)
10000068 6.684 0.000 6.684 0.000 {_struct.unpack}
1 0.002 0.002 548.993 548.993 {execfile}
2 0.000 0.000 0.000 0.000 {len}
1 0.065 0.065 0.065 0.065 {method 'close' of 'mmap.mmap' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 0.000 0.000 {method 'fileno' of 'file' objects}
10003 0.955 0.000 0.955 0.000 {method 'size' of 'mmap.mmap' objects}
2 0.020 0.010 0.020 0.010 {open}
2 0.000 0.000 0.000 0.000 {time.clock}

有没有更快的方法来格式化字符串?

最佳答案

为了更精确地解决该问题,我建议通过将“convertXYZ”设为无操作函数并对结果进行计时来测量文件读取操作。并通过更改“读取”以始终返回一个简单点来测量转换函数,但调用转换和输出的次数与真正读取文件的次数相同。 (可能还有另一次运行,其中最终的转换后输出是空操作。)根据时间的推移,攻击其中一个可能更有意义。

您可以通过将输出写入 Python 的 stdout 并让 shell 执行实际的文件 IO,让本地操作系统为您执行一些交错操作。类似地,通过将​​文件流式传输到标准输入(例如,cat oldformat | python conversion.py > outputfile)

输入和输出文件位于什么类型的存储上?与 Python 代码相比,存储特性对性能的影响可能更大。

更新:鉴于输出是最慢的,并且您的存储非常慢并且在读取和写入之间共享,请尝试添加一些缓冲。来自 the python doc您应该能够通过向 os.open 调用添加第三个参数来添加一些缓冲。尝试一些像 128*1024 这样相当大的尺寸?

关于python - 快速转换大型 (2.1gb+) 二进制文件(纬度/经度/海拔高度至 ECEF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828891/

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