-3

I was wondering,is there a difference in execution time between ++i and i++ in the for loop increment?

1)for(int i=0;i<100;++i)

2)for(int i=0;i<100;i++)

I have heard that the one with the pre-increment uses less registers, and hence, it is faster.

Is it correct?

  • obviously this would depend on the language, but you can test performance in various languages using something like http://jspref.com – sadmicrowave Sep 15 '17 at 14:07
  • 3
    Without knowing which language, specific compiler and optimisation settings - it's impossible to give a definitive answer. The common answer is; "almost all modern compiles can optimise this" and "don't micro-optimise until you have run a profiler and identified a bottleneck - at which point you'll know the answer to your question anyway". –  Sep 15 '17 at 14:07

2 Answers2

2

Not these days no; if you can optimise it in your head then you can bet your bottom dollar that a compiler can also optimise it.

If you are in any doubt, check the generated assembly / bytecode &c.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

This seems to be C code. In this case, there isn't any difference: try compiling those two sources with a compiler and ask it to keep intermediate assembler code.

Then use diff to see if the assembler files differ.

On FreeBSD with clang, on an x86_64 architecture, you can check that both of the files are strictly the same. The pre-increment is done the same way as the post-increment: addl $1, %eax.

Create x.c:

int main() {
  for(int i=0;i<100;++i){}
}

Compile it:

% cc -S x.c
%

look at the code:

        .file   "x.c"
        .text
        .globl  main
        .align  16, 0x90
        .type   main,@function
main:                                   # @main
        .cfi_startproc
# BB#0:
        pushq   %rbp
.Ltmp2:
        .cfi_def_cfa_offset 16
.Ltmp3:
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
.Ltmp4:
        .cfi_def_cfa_register %rbp
        movl    $0, -4(%rbp)
        movl    $0, -8(%rbp)
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        cmpl    $100, -8(%rbp)
        jge     .LBB0_4
# BB#2:                                 #   in Loop: Header=BB0_1 Depth=1
        jmp     .LBB0_3
.LBB0_3:                                #   in Loop: Header=BB0_1 Depth=1
        movl    -8(%rbp), %eax
        addl    $1, %eax
        movl    %eax, -8(%rbp)
        jmp     .LBB0_1
.LBB0_4:
        movl    -4(%rbp), %eax
        popq    %rbp
        ret
.Ltmp5:
        .size   main, .Ltmp5-main
        .cfi_endproc


        .ident  "FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512"
        .section        ".note.GNU-stack","",@progbits

Do the same with --i instead of ++i.

Alexandre Fenyo
  • 4,526
  • 1
  • 17
  • 24
  • 1
    Or put them on http://gcc.godbolt.org/ to easily view the compiler output with clang, icc, msvc, or gcc. (And for x86, ARM, PowerPC, AVR, and some others). – Peter Cordes Sep 15 '17 at 14:10
  • The un-optimized compiler output for an empty loop is only mildly interesting. Even if it had been different, it wouldn't mean `i++` was slower, because optimizing for `-O0` is meaningless and doesn't tell you much about what will happen with `-O3` https://stackoverflow.com/questions/32000917/c-loop-optimization-help-for-final-assignment/32001196#32001196. – Peter Cordes Sep 15 '17 at 14:14
  • Yes, but I've not used -O3 specifically because the loop was empty (like in the question) and I wanted to show the assembler code for post incrementation and pre incrementation too. With -O3, I would have gotten nothing. – Alexandre Fenyo Sep 15 '17 at 14:20
  • The question doesn't show a loop body at all. Note the lack of a semicolon. I think it's trying ask about the general case for any loop. – Peter Cordes Sep 15 '17 at 14:22
  • I did not know godbolt, that seems really interesting. – Alexandre Fenyo Sep 15 '17 at 14:22
  • Anyway, optimizing things that disappear with optimization enabled is not useful. Insert a loop body and use the result as a function return value. Or have a side-effect on memory. e.g. https://godbolt.org/g/WXB3Tw `void foo(int *a) { for(int i=0; i<100 ; i++) a[i]+=i; }`. I boldly predict that pre-increment will compile to the same asm :P – Peter Cordes Sep 15 '17 at 14:30