什么';这是在C#中向文件中写入短[]数组的最佳方法
本文关键字:数组 方法 最佳 什么 文件 | 更新日期: 2023-09-27 17:48:50
我有一个short(short[])数组,需要将其写入文件。做这件事最快的方法是什么?
使用BinaryWriter
static void WriteShorts(short[] values, string path)
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
foreach (short value in values)
{
bw.Write(value);
}
}
}
}
根据Jon B的回答,如果您的文件包含任何其他数据,您可能需要在数据前面加上值的计数。
即:
static void WriteShorts(short[] values, string path)
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
// Write the number of items
bw.Write(values.Length);
foreach (short value in values)
{
bw.Write(value);
}
}
}
}
BinaryFormatter在与基元类型数组(obj.GetType().IsPrimitive)一起使用时,读写速度实际上快了10倍,即不适用于Decimal和String(它们不是基元),当然也不适用于任何其他结构或类,因为它们的速度非常慢。
[Test]
public void TestShortArray()
{
var n = 100000000;
var input = new short[n];
var r = new Random();
for (var i = 0; i < n; i++) input[i] = (short)r.Next();
var bf = new BinaryFormatter();
var sw = new Stopwatch();
using (var ms = new MemoryStream())
{
sw.Start();
bf.Serialize(ms, input);
sw.Stop();
Console.WriteLine("BinaryFormatter serialize: " +
sw.ElapsedMilliseconds + " ms, " + ms.ToArray().Length + " bytes");
sw.Reset();
ms.Seek(0, SeekOrigin.Begin);
sw.Start();
var output = (short[])bf.Deserialize(ms);
sw.Stop();
Console.WriteLine("BinaryFormatter deserialize: " +
sw.ElapsedMilliseconds + " ms, " + ms.ToArray().Length + " bytes");
Assert.AreEqual(input, output);
}
sw.Reset();
using (var ms = new MemoryStream())
{
var bw = new BinaryWriter(ms, Encoding.UTF8, true);
sw.Start();
bw.Write(input.Length);
for (var i = 0; i < input.Length; i++) bw.Write(input[i]);
sw.Stop();
Console.WriteLine("BinaryWriter serialize: " +
sw.ElapsedMilliseconds + " ms, " + ms.ToArray().Length + " bytes");
sw.Reset();
ms.Seek(0, SeekOrigin.Begin);
var br = new BinaryReader(ms, Encoding.UTF8, true);
sw.Start();
var length = br.ReadInt32();
var output = new short[length];
for (var i = 0; i < length; i++) output[i] = br.ReadInt16();
sw.Stop();
Console.WriteLine("BinaryReader deserialize: " +
sw.ElapsedMilliseconds + " ms, " + ms.ToArray().Length + " bytes");
Assert.AreEqual(input, output);
}
}
输出:
BinaryFormatter serialize: 175 ms, 200000028 bytes
BinaryFormatter deserialize: 79 ms, 200000028 bytes
BinaryWriter serialize: 1499 ms, 200000004 bytes
BinaryReader deserialize: 1599 ms, 200000004 bytes
因此,只要您有一个基元类型的数组,或者数组数组,但不是多维数组,就可以使用BinaryFormatter(!)。例如,如果您的数据类型是Point3(double),则应该填充一个double[]并序列化它。仅对复杂/混合类型、字符串、小数和奇异值使用BinaryWriter。
当处理byte[]时,BinaryFormatter和BinaryWriter的性能相同(而且非常快)。如果您能够以一种有效的方式将类型转换为字节数组,那么您可以通过这种方式获得更快的性能。