Re: What the?



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" <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

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • [PATCH] x86-64 kprobes: handle %RIP-relative addressing mode
    ... The existing x86-64 kprobes implementation doesn't cope with the ... an instruction overwritten by a breakpoint. ... instruction that uses the %RIP-relative data addressing mode, ... This patch fixes the problem by recognizing the %RIP-relative addressing ...
    (Linux-Kernel)
  • Re: "Complete exercise" of x86 example?
    ... bit instruction set (excluding 8086-specific and intel-specific quirks ... such as POP CS and AAD/AAM with custom operand). ... something, or screw up a lesser-known addressing mode, or some other ... A complete set of all possible instruction variants and addressing ...
    (comp.lang.asm.x86)
  • Re: What the?
    ... instruction set which had a pre-increment and post-decrement addressing mode built into ... increment and decrement could be built into the instruction address bits (in fact, ... For reference, here are canonical ...
    (microsoft.public.vc.mfc)
  • Re: New ARM Cortex Microcontroller Product Family from STMicroelectronics
    ... The data is embedded inside the instruction, ... into R0, using immediate addressing mode. ... to the ALU using an internal mux in the CPU core it is original harvard. ...
    (comp.arch.embedded)

Loading