Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 1
    As an aside, it also turns out that around an array length of 100 is when .NET's Array.Clear() first begins to beat an explicit loop assignment clearing of an array (setting to false, 0, or null). This is consistent with my similar findings above. These separate benchmarks were discovered online here: manski.net/2012/12/net-array-clear-vs-arrayx-0-performance Commented Dec 7, 2015 at 22:20
  • When you say buffer size; do you mean in bytes, or element count? Commented Jul 8, 2016 at 18:53
  • In my above answer, both "buffer length" and "buffer size" generally refer to element count. Commented Nov 16, 2016 at 7:27
  • 1
    @TodCunningham, you mentioned "source offset by 5 bytes". Array.Copy and Buffer.BlockCopy are strongest when they can operate on machine-word and/or page boundaries. On a 64 bit machine that'll be 4 bytes boundaries. Furthermore, as shown in this post, the overhead of the method call, plus bounds checking, and the lack of locality makes Array.Copy a pretty bad choice for small arrays (< 100 or so items). An (unrolled) loop with bounds-checking off will be much faster, as you also found. Commented May 12, 2020 at 1:29
  • 1
    Interesting answer. Since it was written in 2015 though, I added a new answer with results for .NET 5. Commented May 24, 2022 at 4:40