1

Why is it that the code:

for( i = 0, j = 0; i < 4 ,  j < 3; i++, j++)

is slower than

 for( i = 0, j = 0; i < 4 &&  j < 3; i++, j++)

Elaborating on that some users proposed that two if statemnts take more time than a single if statement with an && operator: I tested it without for loops and it is not true. Two if statements are faster than a single one with a && operator.

Igor Pejic
  • 3,658
  • 1
  • 14
  • 32

4 Answers4

3

The first code is not slower; at least in gcc without optimization. In fact, it should be faster.

When you compile both codes and disassemble them, you will find this for the first code:

cmpl   $0x2,-0x8(%rbp)
jle    26 <main+0x26>

And this for the second one:

cmpl   $0x3,-0x4(%rbp)
jg     44 <main+0x44>
cmpl   $0x2,-0x8(%rbp)
jle    26 <main+0x26>

In the first example, gcc evaluates just the second part, because the first one has no effect and is not used in the comparison. In the second one, it has to check for the first one, and then, if true, check the second one.

So, in the general case, the first example should be faster than the first one. If you find the first slower, maybe your way to measure it was not 100% correct.

Juan Cespedes
  • 1,299
  • 12
  • 27
  • this is not perfectly valid.....the thing you are explaining is something that happens in case of **&** , but not **&&** – nobalG Aug 28 '14 at 08:55
1

Their may be no change in execution time but may very the number of iterations since :

If we put comma separated condition in for loop,it evaluates the value of the last one. So basically whichever condition you write first, it will be disregarded, and the second one will be checked. So j = 0; i < 4 will always check for i<4 where as i < 4 && j < 3 will examine and return true if and only if both the conditions are true.

Reference

Community
  • 1
  • 1
0

If we do check the assembly of the code you have you may see the difference

program

int main()
{
    int x,y;
    for(x=0,y=0;x<4,y<5;x++,y++);

    printf("New one");

    for(x=0,y=0;x<4 && y<5;x++,y++);

}

command to get assembly : gcc -S <program name>

Assembly

          .file   "for1.c"
    .section    .rodata
.LC0:
    .string "New one"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $32, %esp
    movl    $0, 24(%esp)
    movl    $0, 28(%esp)
    jmp .L2
.L3:
    addl    $1, 24(%esp)
    addl    $1, 28(%esp)
.L2:
    cmpl    $4, 28(%esp) //Here only one condition
    jle .L3
    movl    $.LC0, (%esp)
    call    printf
    movl    $0, 24(%esp)
    movl    $0, 28(%esp)
    jmp .L4
.L6:
    addl    $1, 24(%esp)
    addl    $1, 28(%esp)
.L4:
    cmpl    $3, 24(%esp) //First Condition
    jg  .L7
    cmpl    $4, 28(%esp) //Second Condition
    jle .L6
.L7:
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section    .note.GNU-stack,"",@progbits

So, it is clear if we have 2 condition then it will be more time taking.

pradipta
  • 1,718
  • 2
  • 13
  • 24
-1

the first option one is two ifs, the second option is a mathematical equation and one if, which is usually faster, here you save one if by doing a calculation, that costs less process time.

first option -> if() && if(),

second option-> if(() && ())

shoham
  • 293
  • 1
  • 9