State of “memset” functionality in C++ with modern compilers

前端 未结 12 1747
我寻月下人不归
我寻月下人不归 2021-02-12 16:03

Context:

A while ago, I stumbled upon this 2001 DDJ article by Alexandrescu: http://www.ddj.com/cpp/184403799

It\'s about comparing various ways to initialize

12条回答
  •  逝去的感伤
    2021-02-12 16:26

    The DDJ article acknowledges that memset is the best answer, and much faster than what he was trying to achieve:

    There is something sacrosanct about C's memory manipulation functions memset, memcpy, and memcmp. They are likely to be highly optimized by the compiler vendor, to the extent that the compiler might detect calls to these functions and replace them with inline assembler instructions — this is the case with MSVC.

    So, if memset works for you (ie. you are initializing with a single byte) then use it.

    Whilst every millisecond may count, you should establish what percentage of your execution time is lost to setting memory. It is likely very low (1 or 2%??) given that you have useful work to do as well. Given that the optimization effort would likely have a much better rate of return elsewhere.

提交回复
热议问题