Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an array of doubles to a file very fast in C#?

Tags:

c#

I want to write something like this to a file:

FileStream output = new FileStream("test.bin", FileMode.Create, FileAccess.ReadWrite);
BinaryWriter binWtr = new BinaryWriter(output);
double [] a = new double [1000000];  //this array fill complete

for(int i = 0; i < 1000000; i++)
{
    binWtr.Write(a[i]);
}

And unfortunately this code's process last very long! (in this example about 10 seconds!)

The file format is binary.

How can I make that faster?

like image 617
MHSFisher Avatar asked Sep 21 '14 17:09

MHSFisher


2 Answers

You should be able to speed up the process by converting your array of doubles to an array of bytes, and then write the bytes in one shot.

This answer shows how to do the conversion (the code below comes from that answer):

static byte[] GetBytes(double[] values) {
    var result = new byte[values.Length * sizeof(double)];
    Buffer.BlockCopy(values, 0, result, 0, result.Length);
    return result;
}

With the array of bytes in hand, you can call Write that takes an array of bytes:

var byteBuf = GetBytes(a);
binWtr.Write(byteBuf);
like image 105
Sergey Kalinichenko Avatar answered Nov 11 '22 08:11

Sergey Kalinichenko


You're writing the bytes 1 by 1, of course it's going to be slow.

You could do the writing in memory to an array and then write the array to disk all at once like this :

var arr = new double[1000000];
using(var strm = new MemoryStream())
using (var bw = new BinaryWriter(strm)) 
{ 
    foreach(var d in arr)
    {
        bw.Write(d);
    }
    bw.Flush();
    File.WriteAllBytes("myfile.bytes",strm.ToArray());
}
like image 1
Ronan Thibaudau Avatar answered Nov 11 '22 09:11

Ronan Thibaudau