Re: What the?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Tue, 12 Feb 2008 23:10:55 -0500
Actually a++ and ++a generate the same code in this context. There is a common myth about
the relative efficiency of these operations; this mythology is largely based on the PDP-11
instruction set which had a pre-increment and post-decrement addressing mode built into
the instruction set. On the PDP-11, a++ and --a were the preferred forms because the
increment and decrement could be built into the instruction address bits (in fact, in the
early versions of the C compiler a++ existed but ++a was a syntax error, and a-- did not
exist and was a syntax error, but --a compiled. Eventually, the symmetry was introduced,
and the a++/++a controversy arose. In modern architectures that do not possess
auto-increment addressing modes, and in the presence of optimizing compilers, this rarely
matters, and when it does, remember that a 2.8GHz Pentium4 will execute a single extra
instruction in 175ps, not really a big deal (that's a 350ps clock dispatching two integer
instructions per clock cycle).
; 12 : for(int i = 0; i < count; i++)
; 13 : DoSomething(data, i);
$L9628:
00011 56 push esi
00012 53 push ebx
00013 e8 00 00 00 00 call ?DoSomething@@YAXPADH@Z ; DoSomething
00018 83 c4 08 add esp, 8
0001b 46 inc esi
0001c 3b f7 cmp esi, edi
0001e 7c f1 jl SHORT $L9628
00020 5b pop ebx
$L9630:
; 18 : for(int i = 0; i < count; ++i)
; 19 : DoSomething(data, i);
$L9636:
00011 56 push esi
00012 53 push ebx
00013 e8 00 00 00 00 call ?DoSomething@@YAXPADH@Z ; DoSomething
00018 83 c4 08 add esp, 8
0001b 46 inc esi
0001c 3b f7 cmp esi, edi
0001e 7c f1 jl SHORT $L9636
00020 5b pop ebx
$L9638:
On Tue, 12 Feb 2008 23:47:47 +0100, "Giovanni Dicanio" <giovanni.dicanio@xxxxxxxxxxx>
wrote:
Joseph M. Newcomer [MVP]
"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> ha scritto nel messaggio
news:aj44r31v925m1ko8ilhmujghppeceu8n8h@xxxxxxxxxx
vector <float>::iterator iter;
for(iter = DataArray.begin(); iter DataArray.end(); iter++)
{
CString Msg;
Msg.Format(_T("%2.5f\n"), *iter);
DatFile.WriteString(Msg);
}
To add to what Joe correctly wrote:
1. it is possible to iterate std::vector also using integers (like CArray
container), e.g.
for ( size_t i = 0; i < DataArray.size(); i++ )
2. It is possible also to use 'const_iterator' (not just 'iterator'), if you
are using the array data in read-only mode (e.g. to print array content).
3. Considering Joe's sample code listed above, I think that '++iter' is more
efficient than 'iter++' (but, to be sure about that, I would prefer
listening to some of the C++ gurus like Doug).
Giovanni
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: What the?
- From: Giovanni Dicanio
- Re: What the?
- From: Doug Harrison [MVP]
- Re: What the?
- References:
- What the?
- From: Ron H.
- Re: What the?
- From: Joseph M . Newcomer
- Re: What the?
- From: Giovanni Dicanio
- What the?
- Prev by Date: Re: Any info about "Thread has exited with code 32772 (0x8004)"?
- Next by Date: Re: What the?
- Previous by thread: Re: What the?
- Next by thread: Re: What the?
- Index(es):
Relevant Pages
|
Loading