Reading arrays from a stream

  • Thread starter Thread starter Stefan L
  • Start date Start date
S

Stefan L

Hi Everybody,

I want to read an array of doubles from a BinaryReader.

Therefore I have the following loop:

int size = r.ReadInt32();
double[] values = new double[size];
for (int i = 0; i < size; i++)
values = r.ReadDouble();

But this is unbelievable slow compared to reading the size,
instanciating the array and then read the values en block as I did it
before in VB6 (File Get ...)

Is there a way to read an entire array from a Reader or Stream or
something similar to speed this up? We said we wanted to stay in managed
code, so I do not want to write a C-Function to accomplish this.

TIA,
Stefan
 
Is there a way to read an entire array from a Reader or Stream or
something similar to speed this up?

I haven't checked if it's faster or not, but you can try reading
size*sizeof(double) bytes into a byte array instead with ReadBytes(),
then use Buffer.BlockCopy to copy it to a double[].



Mattias
 
Stefan L said:
Hi Everybody,

Can't you read the values as and when you need them,
rather than up front?

Alternatively, can't you string all the values together in
one long string and write and read them in one go?
My experience is that that goes super fast.
 
Back
Top