5

In the follow two code snippets, is there actually any different according to the speed of compiling or running?

for (int i = 0; i < 50; i++)
{
    if (i % 3 == 0)
        continue;

    printf("Yay");
}

and

for (int i = 0; i < 50; i++)
{
    if (i % 3 != 0)
        printf("Yay");
}

Personally, in the situations where there is a lot more than a print statement, I've been using the first method as to reduce the amount of indentation for the containing code. Been wondering for a while so found it about time I ask whether it's actually having an effect other than visually.

Reply to Alf (i couldn't get code working in comments...)

More accurate to my usage is something along the lines of a "handleObjectMovement" function which would include

for each object
    if object position is static
        continue

    deal with velocity and jazz

compared with

for each object
    if object position is not static
        deal with velocity and jazz

Hence me not using return. Essentially "if it's not relevant to this iteration, move on"

Joel
  • 1,580
  • 4
  • 20
  • 33
  • I think dealing with trivial cases or error cases first in a function, using `return` to **bail out**, is OK. But `continue` means that the reader of the code must use extra effort to analyze. Remember that writing source code is not about communicating to the compiler what it should do, so much as communicating to others or your later self what your *intent* is. – Cheers and hth. - Alf Apr 21 '14 at 11:34
  • 3
    A good compiler should optimize equivalent source into same assembly code. Whether that actually happens in any given case depends on too many things to know for certain in advance. So why don't you just check it yourself? Examine the generated assembly and/or measure the timings and you'll have your answer. – Branko Dimitrijevic Apr 21 '14 at 11:37
  • Two reasons Branko, the first is bad, but the second I feel is pretty good :P 1) I am lazy 2) It is useful to have these sort of questions just a google away. Now anyone else who wonders they same has a SO page they can go to. – Joel Apr 21 '14 at 11:53
  • Alf, I edited my post to show why continue was relevant while return wasn't. Thanks for the replies guys. – Joel Apr 21 '14 at 12:01

3 Answers3

8

The behaviour is the same, so the runtime speed should be the same unless the compiler does something stupid (or unless you disable optimisation).

It's impossible to say whether there's a difference in compilation speed, since it depends on the details of how the compiler parses, analyses and translates the two variations.

If speed is important, measure it.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

If you know which branch of the condition has higher probability you may use GCC likely/unlikely macro

Community
  • 1
  • 1
AlexT
  • 1,413
  • 11
  • 11
1

How about getting rid of the check altogether?

for (int t = 0; t < 33; t++)
{
    int i = t + (t >> 1) + 1;

    printf("%d\n", i);
}
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 1
    I think he was really asking about the compiler behavior not this particular code – gbtimmon Apr 21 '14 at 11:52
  • 1
    Still, you'd be surprised how many uses of continue can be removed by writing smarter code. Do you have a different, more complicated example in mind? – fredoverflow Apr 21 '14 at 11:55