Well, why don't we take a look at the generated assembly code, full optimization in VS 2010.
char x[500]; char y[500]; int i; memset(x, 0, sizeof(x) ); 003A1014 push 1F4h 003A1019 lea eax,[ebp-1F8h] 003A101F push 0 003A1021 push eax 003A1022 call memset (3A1844h)
And your loop ...
char x[500]; char y[500]; int i; for( i = 0; i < 500; ++i ) { x[i] = 0; 00E81014 push 1F4h 00E81019 lea eax,[ebp-1F8h] 00E8101F push 0 00E81021 push eax 00E81022 call memset (0E81844h) /* note that this is *replacing* the loop, not being called once for each iteration. */ }
So, in this compiler, the generated code will be exactly the same. memset is fast, and the compiler is smart enough to know that you are doing the same thing as calling memset once, so it does it for you.
If the compiler actually left the loop as-is, then it will probably be slower as you can set more than one block of byte size at a time (i.e. you can expand your loop a bit. memset will be at least as fast as a naive implementation, such as a loop. Try it in a debug assembly and you will notice that the loop is not replaced.
However, it depends on what the compiler does for you. Looking at the showdown, there is always a good way to know exactly what is going on.
Ed S. Sep 09 '11 at 21:45 2011-09-09 21:45
source share