我需要快速存储/加载可能较大的 ushort[,,]
、byte[,,]
或 double[,,]
数组。
我尝试过简单的方法,简单地将数组元素逐个扫描到 FileStream fs
中:
for (p = 0; p < Planes; p++)
{
for (y = 0; y < Height; y++)
{
for (x = 0; x < Width; x++)
{
fs.Write(BitConverter.GetBytes(array1[p, y, x]), 0, 8);
}
}
}
我用 BinaryFormatter
替换了这个循环,它的性能更好:
bf.Serialize(fs, array1);
我运行了一个小型基准测试存储,然后加载 3 x 1024 x 768 double 组 10 次,结果如下:
朴素方法:10 628 毫秒
BinaryFormatter 方法:8 722 毫秒
有没有更快的方法?使用指针?将数组序列化为一维,然后刷新到文件?
感谢您的任何建议。
最佳答案
使用MemoryStream
写入数据,然后copyTo
你的FileStream
ms.copyTo(fs)
它会加速你对硬盘的IO操作
关于c# - 快速加载/保存多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17647015/