How to quickly zero out an array?

后端 未结 7 966
青春惊慌失措
青春惊慌失措 2020-12-08 18:30

I am currently doing it in a for loop, and I know in C there is the ZeroMemory API, however that doesn\'t seem to be available in C#. Nor does the somewhat equivalent Array.

7条回答
  •  清歌不尽
    2020-12-08 18:44

    Several people have posted answers, then deleted them, saying that in any language a for loop will be equally performant as a memset or FillMemory or whatever.

    For example, a compiler might chunk it into 64-bit aligned pieces to take advantage of a 64bit zero assignment instruction, if available. It will take alignment and stuff into consideration. Memset's implementation is certainly not trivial.

    one memset.asm. Also see memset-is-faster-than-simple-loop.html.

    Never underestimate the infinite deviousness of compiler and standard library writers.

提交回复
热议问题