Re: Vectors vs Arrays performance
The assy presented shows exactly that.
The assembly shows that when you surround a single access with
inline assembler and accesses to a volatile, the compiler
doesn't optimize very well. Something I was aware of before
even looking at the assembler.
In the presented case the compiler did a good job, and I'm sure it would
produce the same code for many different layouts using other tricks
against removal.
A version without any "scary" volatile or inline assembler:
void foo(int);
int main()
{
void (*fp)(int) throw() = &foo;
int a[10];
std::vector<int> v(10);
fp(0);
fp(a[0]);
fp(v[0]);
}
produces:
; 15 : fp(a[0]);
0004f 8b 44 24 28 mov eax, DWORD PTR _a$[esp+96]
00053 83 c4 04 add esp, 4
00056 50 push eax
00057 e8 00 00 00 00 call ?foo@@YAXH@Z ; foo
; 16 : fp(v[0]);
0005c 8b 44 24 1c mov eax, DWORD PTR _v$[esp+100]
00060 8b 08 mov ecx, DWORD PTR [eax]
00062 83 c4 04 add esp, 4
00065 51 push ecx
00066 e8 00 00 00 00 call ?foo@@YAXH@Z ; foo
0006b 83 c4 04 add esp, 4
Again the pertinent bits are:
mov eax, DWORD PTR _a$[esp+96]
vs.
mov eax, DWORD PTR _v$[esp+100]
mov ecx, DWORD PTR [eax]
One extra instruction for the std::vector version. And for the avoidance of
doubt I repeat again that I realize that the register can be re-used in a
subsequent loop a fact I stated in my original post if you care to read it
properly Mr James Kanze.