XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Using unsafe code for optimization

Last post 5/1/2007 12:43 PM by Shawn Hargreaves. 2 replies.
  • 5/1/2007 6:38 AM

    Using unsafe code for optimization

    I have rarely actually needed to use "unsafe" code for anything in c#, but I understand that it can be used for performance optimization.

    Is it worth looking into using unsafe code for specific resource hungry methods? Or is the pain not worth it?

    I don't like messing up my code, but if I could hide away any fancy unsafe code in a few methods then I would still be happy. I suppose I am wondering whether the performance gains are ever going to be significant?
  • 5/1/2007 7:33 AM In reply to

    Re: Using unsafe code for optimization

    Answer
    Reply Quote
    Yes, unsafe code can produce significant performance gains whenever you need to read or write a lot of data and process it, such as in direct manipulation of vertex buffers. Without using pointers, you would have to read the data into arrays and you incur extra performance hits for things like array bounds checks.
  • 5/1/2007 12:43 PM In reply to

    Re: Using unsafe code for optimization

    Unsafe code only really helps in a few very specific kinds of situation, though. It isn't a generally useful way to speed up your entire codebase. As a general rule, any time you find yourself doing a ton of data manipulation shuffling things around inside a single array (for instance generating a dynamic vertex buffer, or writing directly into a generated texture, or mixing audio data) that's where unsafe code might be able to help.
    XNA Framework Developer - blog - homepage
Page 1 of 1 (3 items) Previous Next