【发布时间】:2010-09-29 18:28:57
【问题描述】:
我有一个使用 50 字节数据结构的文件格式(.STL,立体光刻,结构是标准的,不能更改。不要与标准模板库混淆)。从HD直接读取会导致错误的数据被读取为50字节不是4的倍数。
在整个 50 字节结构中,我只需要其中的 36 个字节。我现在用来提取数据的方法是手动将文件的读取位置偏移到下一组数据开始的位置,将36个字节读取到临时变量中,然后将数据转储到临时变量中进入它在阵列上的正确位置。
这是代码块:
threePoints* output = new threePoints [numTriangles]; // create an array to hold the entire file
threePoints* temp = new threePoints [1]; // temp variable to pull data out of each "cell"
// extract each triangle individualy
for (int i = 0; i < numTriangles; i++)
{
stlFile.seekg (96 + i * 50, ios::beg); //read vertex data and put them into tempoary array
// offset = 80 header + 4 #triangles + 12 normal vector
stlFile.read(reinterpret_cast<char*>(temp), (36)); // read the next 36 data blocks into holder, 3 points * 3 axis * 4 bytes per float
output[i] = temp[0]; // dump values in holder into proper array
}
此方法有效,但速度较慢。我该如何做才能使其更快、更高效?
编辑:我知道手动禁用字节对齐可以解决这个问题,但我需要大量使用生成的数组并对其进行多次迭代。我被告知禁用字节对齐会导致性能问题,因此我避免了它。
【问题讨论】:
标签: c++ performance file-io