What is the best way to write a short [] array to a file in C #?

I have an array of short circuits (short []) that I need to write to a file. What is the fastest way to do this?

+6
c # filestream
source share
3 answers

Use 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); } } } } 
+11
source share

In response to Jon B's answer, if your file contains any other data, you can prefix the data with a value counter.

i.e:.

 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); } } } } 
+2
source share

BinaryFormatter is actually about 10 times faster both for reading and writing when used with arrays of primitive types (obj.GetType (). IsPrimitive), that is, not for Decimal and String (which are not primitive) and, of course , not for any other structure or class where it is terribly slow instead.

 [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); } } 

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 

So use BinaryFormatter whenever you have an array of primitive type or an array of arrays but not multi-sized arrays (!). If your data type is, for example, Point3 (double), you must swap the double [] and serialize it. Use only BinaryWriter for complex / mixed types, strings, decimals, and singular values.

When you work with byte [], BinaryFormatter and BinaryWriter are equally efficient (and very fast). If you can convert your type to a byte array in an efficient way, you can get even better performance this way.

+1
source share

All Articles