True Unsafe Code Performance

前端 未结 7 603

I understand unsafe code is more appropriate to access things like the Windows API and do unsafe type castings than to write more performant code, but I would like to ask yo

相关标签:
7条回答
  • 2020-11-30 11:25

    In this response to an unrelated Stack Overflow article that has been viewed 881,000 times, benchmarks for various byte[] to hex string algorithms are shown.

    The fastest is an unsafe implementation of the same safe code that holds the #2 spot. The unsafe is over twice as fast in this comparison.

    The details of this algorithm are here.

    0 讨论(0)
  • 2020-11-30 11:30

    I am using unsafe code for video manipulation code. In such code you want it to run as fast as possible without internal checks on values etc. Without unsafe attributes my could would not be able to keep up with the video stream at 30fps or 60 fps. (depending on used camera).

    But because of speed its widely used by people who code graphics.

    0 讨论(0)
  • 2020-11-30 11:32

    Well, I would suggest to read this blog-post: MSDN blogs: Array Bounds Check Elimination in the CLR

    This clarifies how bounds-checks are done in C#. Moreover, Thomas Bratts tests seem to me useless (looking at the code) since the JIT removes in his 'save' loops the bound-checks anyway.

    0 讨论(0)
  • 2020-11-30 11:38

    To all that are looking at these answers I would like to point out that even though the answers are excellent, a lot has changed sins the answers have been posted.

    Please note that .net has changed quite a bit and one now also has the possibility to access new data types like vectors, Span, ReadOnlySpan as well as hardware specific libraries and classes like those found in System.Runtime.Intrinsics in core 3.0

    Have a look at this blog post to see how hardware optimized loops could be used and this blog how to fallback to safe methods if optimal hardware is not available.

    0 讨论(0)
  • 2020-11-30 11:39

    A good example is image manipulations. Modifying the Pixels by using a pointer to their bytes (which requires unsafe code) is quite a bit faster.

    Example: http://www.gutgames.com/post/Using-Unsafe-Code-for-Faster-Image-Manipulation.aspx

    That being said, for most scenarios, the difference wouldn't be as noticeable. So before you use unsafe code, profile your application to see where the performance bottlenecks are and test whether unsafe code is really the solution to make it faster.

    0 讨论(0)
  • 2020-11-30 11:42

    Some Performance Measurements

    The performance benefits are not as great as you might think.

    I did some performance measurements of normal managed array access versus unsafe pointers in C#.


    Results from a build run outside of Visual Studio 2010, .NET 4, using an Any CPU | Release build on the following PC specification: x64-based PC, 1 quad-core processor. Intel64 Family 6 Model 23 Stepping 10 GenuineIntel ~2833 Mhz.

    Linear array access
     00:00:07.1053664 for Normal
     00:00:07.1197401 for Unsafe *(p + i)
    
    Linear array access - with pointer increment
     00:00:07.1174493 for Normal
     00:00:10.0015947 for Unsafe (*p++)
    
    Random array access
     00:00:42.5559436 for Normal
     00:00:40.5632554 for Unsafe
    
    Random array access using Parallel.For(), with 4 processors
     00:00:10.6896303 for Normal
     00:00:10.1858376 for Unsafe
    

    Note that the unsafe *(p++) idiom actually ran slower. My guess this broke a compiler optimization that was combining the loop variable and the (compiler generated) pointer access in the safe version.

    Source code available on github.

    0 讨论(0)
提交回复
热议问题