The answer depends on the implementation of the standard library.
MSVC, for example, has several implementations of std::fill_n based on the types of what you are trying to fill.
Call std::fill_n with char* or signed char* or unsigned char* , and it will directly call memset to populate the array.
inline char *_Fill_n(char *_Dest, size_t _Count, char _Val) {
If you call another type, it fills the loop:
template<class _OutIt, class _Diff, class _Ty> inline _OutIt _Fill_n(_OutIt _Dest, _Diff _Count, const _Ty& _Val) {
The best way to determine the overhead on your particular compiler and standard library implementation is with a code profile with both calls.
source share