2

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

1
  • Are you reading them from one big file or 400k small files? If you have all in one file reading should be quite fast I think Commented Feb 11, 2009 at 8:32

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately they do it "one by one" here, which is exactly what I want to avoid
The fixed solution seems to be what I was looking for! thanks!
Didn't compile on Mono: "Cannot implicitly convert type 'YourStruct[]' to YourStruct"
0

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.

3 Comments

Thanks, but "Serialize" is slow compared to direct memory access, and the other links use "PtrToStructure" to copy "one by one" which is exactly what I want to avoid.
Sorry pablo, I had computer trouble yesterday. So I couldn't follow up. I am not sure how to achieve what you want. All the best with that.
I hope you find out. If you do, please post an answer here. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.