How to write an array of doubles to a file very fast in C#? -
i want write file:
filestream output = new filestream("test.bin", filemode.create, fileaccess.readwrite); binarywriter binwtr = new binarywriter(output); double [] = new double [1000000]; //this array fill complete for(int = 0; < 1000000; i++) { binwtr.write(a[i]); }
and unfortunately code's process last long! (in example 10 seconds!)
the file format binary.
how can make faster?
you should able speed process converting array of doubles array of bytes, , write bytes in 1 shot.
this answer shows how conversion (the code below comes 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 array of bytes in hand, you can call write
takes array of bytes:
var bytebuf = getbytes(a); binwtr.write(bytebuf);
Comments
Post a Comment