2

Or is it the same in terms of performance?

For example, which is faster?

int a = 1, b = 2;
for (int i = 0; i < 10; ++i) {
    a = a + 1;
    b = b + 1;
}

or

for (int i = 0; i < 10; ++i) {
    a = a + 1;
}
for (int i = 0; i < 10; ++i) {
    b = b + 1;
}

Note: I changed my examples, given a lot of people seem hung up on the statements inside them rather than the purpose of my question.

Seb0029
  • 69
  • 8
  • 3
    Both of the examples have identical observable behavior (which is to say, no observable behavior whatsoever). Any difference in speed would depend on the compiler used, the build flags, the execution environment, basically it depends on your the specifics of your use case. – François Andrieux Oct 05 '20 at 20:11
  • Did you try [benchmarking](https://quick-bench.com/) this? – Paul Sanders Oct 05 '20 at 20:11
  • Those loops will be optimized away completely. – πάντα ῥεῖ Oct 05 '20 at 20:12
  • So the compiler will optimize it anyway. – Seb0029 Oct 05 '20 at 20:17
  • 3
    *"I changed my examples, given a lot of people seem hung up on the statements inside them rather than the purpose of my question."* That is because the answer to your question can depend on the statements in the loops. Edit : With the edit the two loops are [still probably identical in performance](https://godbolt.org/z/s4j9bM). – François Andrieux Oct 05 '20 at 20:22
  • 3
    I would prefer the first example if `i` has the same meaning for both operations for clarity. I would use the second example if `i` has a different meaning for the two operations. You should pick whichever approach makes the intention of the code easier to understand. Provided both define the same behavior most compilers will correctly merge or split the loops if there is a performance impact. – François Andrieux Oct 05 '20 at 20:27
  • 3
    Here is one great answer to this: https://stackoverflow.com/questions/51021262/for-loop-efficiency-merging-loops – Vlad Feinstein Oct 05 '20 at 20:31
  • 1
    If you want to know what an optimizing compiler does with this code then look [here on Compiler Explorer](https://godbolt.org/z/fMrnf8). Whether it's one loop or two they completely evaporate and are reduced to a single instruction. Write clear code that expresses your intent and let the compiler worry about the small optimizations. – Blastfurnace Oct 05 '20 at 20:43
  • "If you want to know what an optimizing compiler does with this code..." Lol, it compiles to identical code. Thanks. But yeah, depends on what the statments are. – Seb0029 Oct 05 '20 at 21:05

3 Answers3

2

Both of your examples do nothing at all and most compilers will optimize them both to the same thing -- nothing at all.

Update: Your two new examples are obviously equivalent. If any compiler generated better code for one than the other, then it's a poor quality compiler and you should just use a better compiler.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 3
    Is it not better to write as comment and vote to close the question? – Tony Tannous Oct 05 '20 at 20:19
  • Lol no. Why do you want to close my question?! Just say what you think is wrong with it and I'll correct. I have corrected already. Sometimes moderators on these forums just want to exercise their godlike powers over those who are just trying to find an answer. I wonder if I can program a Python script to post the same question every 5 minutes ;) – Seb0029 Oct 05 '20 at 21:00
  • 2
    @Seb0029 Questions are not usually closed by moderators, they are usually closed by the community. *"Why do you want to close my question?!"* There are [several common causes](https://stackoverflow.com/help/closed-questions) for questions being closed. In a general sense, they are closed because they do not follow the guidelines of this site. I personally don't believe this question should be closed. So far, it has only received one vote to close for "Needs more details" but this late, with two answers and this amount of activity it probably won't be closed. – François Andrieux Oct 05 '20 at 21:27
0

As people have pointed out, the compiler will optimize regardless of which way I go with but it really depends on what statements are inside the loop(s).

Seb0029
  • 69
  • 8
0

The performance depends on the contents of the loops.

Let's decompose the for loop. A for loop is comprised of:

  1. Initialization
  2. Comparison
  3. Incrementing
  4. Content (statements)
  5. Branching

Let us define a comparison as a compare instruction (to set the processor status bits) and a branch (to take advantage of the processor status bits).

Processors are at their happiest when they are executing data instructions. The processor manipulates the data, then processes the next instruction in the pipeline (cache).

The processors don't like sections 2) Comparison and 5) Branching (to the top of the loop). Branching means that the processor has stop processing data and execute logic to determine if the instruction cache needs to be replaced or not. This time could be spent processing data instructions.

The goal to optimizing a for loop is to reduce the branching. The secondary one is to optimize the data cache / memory accesses. A common optimization technique is loop unrolling, or basically placing more statements inside the for loop. As a measurement, you can take the overhead of the for loop and divide by the quantity of statements inside the loop.

According to the above information, your first loop (with both assignment statements) would be more efficient, since there are more data instructions per loop; less overhead overall.

Edit 1: The Parallel Environment
However, your second example may be faster. The compiler could set up both loops to run in parallel (either through instructions or actual parallel tasks). Since both loops are independent, they can be run at the same time or split between CPU cores. Processors have instructions that can perform common operations on multiple memory locations. Your first example, makes this a little more difficult because it requires more analyzation from the compiler. Since the loops on the second example are simpler, the compiler's analyzation is also simpler.

Also, the quantity of iterations also plays a factor. For small quantities, the loops should perform the same or have negligible differences. For large quantities of iterations, there may be some timing differences.

In summary: PROFILE. BENCHMARK. The only true answer depends on measurements. They may vary depending on the applications being run at the same time, the amount of memory (both RAM and hard drive), the quantity of CPU cores and other items. Profile and Benchmark on your system. Repeat on other systems.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154