is memset more efficient than for loop. so if i have
char x; memset(x,0,sizeof(x));
char x; for(int i = 0 ; i < 500 ; i ++) x[i] = 0;
which one is more efficient and why? is there any special instruction in hardware to do block level initialization.
memset will be much faster than that loop. Note how you treat one character at a time, but those functions are so optimized that set several bytes at a time, even using, when available, MMX and SSE instructions.
I think the paradigmatic example of these optimizations, that go unnoticed usually, is the GNU C library
strlen function. One would think that it has at least O(n) performance, but it actually has O(n/4) or O(n/8) depending on the architecture (yes, I know, in big O() will be the same, but you actually get an eighth of the time). How? Tricky, but nicely: strlen.