loading files into array

  • Thread starter Thread starter Baris
  • Start date Start date
B

Baris

What is the best and speedy way of loading files into an array?

i have an

int[] MyArray = new int[1000000];

and a file (size is 4000000 byte)
 
What is the best and speedy way of loading files into an array?

I would

- Open a FileStream for the file
- Construct a BinaryReader around it
- Read the data into a byte[4000000] with BinaryReader.Read
- Use Buffer.BlockCopy to copy it over to the int[]

assuming that there's enough memory available and duplicating the data
isn't a problem.



Mattias
 
Baris said:
What is the best and speedy way of loading files into an array?

i have an

int[] MyArray = new int[1000000];

and a file (size is 4000000 byte)

Well, I would ask why you want to load it into an array of ints instead
of bytes to start with. Is your data *actually* just a series of
1000000 ints?

Anyway, see http://www.pobox.com/~skeet/csharp/readbinary.html for more
information about reading the file.
 
Yes,

my data is a series of 1000000 ints.


Jon Skeet said:
Baris said:
What is the best and speedy way of loading files into an array?

i have an

int[] MyArray = new int[1000000];

and a file (size is 4000000 byte)

Well, I would ask why you want to load it into an array of ints instead
of bytes to start with. Is your data *actually* just a series of
1000000 ints?

Anyway, see http://www.pobox.com/~skeet/csharp/readbinary.html for more
information about reading the file.
 
Baris said:
my data is a series of 1000000 ints.

Well, *if* they're in the appropriate format, just calling
BinaryReader.ReadInt32 1000000 times is probably a reasonable bet.

Alternatively, read it in as a byte array in the normal way and then
use Buffer.BlockCopy to copy it into an int array.
 
Back
Top