stuff i do at work

(maybe i’ll make less mistakes if i remember some)

marshalling byte arrays into data structures

Posted by noah on May 21, 2008

After writing a program and benchmarking, it turns out that Marshal.PtrToStructure took up about 1/4 of my app’s cpu consumption!  It just so happened that a few days ago, an article was written on this exact subject (http://www.codeproject.com/KB/dotnet/ReadingStructures.aspx?display=Print)

Unfortuantely, I couldn’t use his best optimization (60x faster) because I used a class, which is managed, so the pointer stuff doesn’t work.

Using fixed instead of GCHandle yielded a 20% perf gain.  Doing the exact operations on structs was 2x faster for GCHandle and 3x faster for “fixed”.

Overall, my perf isn’t that bad, so it’s not worth doing these things.  One option was to make my class into a wrapper class for a struct, but that seems like just too much effort.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>