Assuming that we have a T myarray[100] with T = int, unsigned int, long long int or unsigned long long int, what is the fastest way to reset all its content to zero (not only for initialization but to reset the content several times in my program)? Maybe with memset?
Same question for a dynamic array like T *myarray = new T[100].
memset(from<string.h>) is probably the fastest standard way, since it’s usually a routine written directly in assembly and optimized by hand.By the way, in C++ the idiomatic way would be to use
std::fill(from<algorithm>):which may be optimized automatically into a
memset; I’m quite sure that it will work as fast asmemsetforints, while it may perform slightly worse for smaller types if the optimizer isn’t smart enough. Still, when in doubt, profile.