Reputation:
I've seen here , and also googling for "marshal" several ways to convert a byte array to a struct.
But what I'm looking for is if there is a way to read an array of structs from a file (ok, whatever memory input) in one step?
I mean, load an array of structs from file normally takes more CPU time (a read per field using a BinaryReader) than IO time. Is there any workaround?
I'm trying to load about 400K structs from a file as fast as possible.
Thanks
pablo
Upvotes: 2
Views: 3149
Reputation: 4175
Following URL may be of interest to you.
http://www.codeproject.com/KB/files/fastbinaryfileinput.aspx
Or otherwise I think of pseudo code like the following:
readbinarydata in a single shot and convert back to structure..
public struct YourStruct
{
public int First;
public long Second;
public double Third;
}
static unsafe byte[] YourStructToBytes( YourStruct s[], int arrayLen )
{
byte[] arr = new byte[ sizeof(YourStruct) * arrayLen ];
fixed( byte* parr = arr )
{
* ( (YourStruct * )parr) = s;
}
return arr;
}
static unsafe YourStruct[] BytesToYourStruct( byte[] arr, int arrayLen )
{
if( arr.Length < (sizeof(YourStruct)*arrayLen) )
throw new ArgumentException();
YourStruct s[];
fixed( byte* parr = arr )
{
s = * ((YourStruct * )parr);
}
return s;
}
Now you can read bytearray from the file in a single shot and convert back to strucure using BytesToYourStruct
Hope you can implement this idea and check...
Upvotes: 1
Reputation: 5221
I found a potential solution at this site - http://www.eggheadcafe.com/software/aspnet/32846931/writingreading-an-array.aspx
It says basically to use Binary Formatter like this:
FileStream fs = new FileStream("DataFile.dat", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, somestruct);
I also found two questions from this site - Reading a C/C++ data structure in C# from a byte array and How to marshal an array of structs - (.Net/C# => C++)
I haven't done this before, being a C# .NET beginner myself. I hope this solution helps.
Upvotes: 0