-2

What would be more efficient?

Pseudocode:

    void function(x) {
    if (x == arg1) {
        execute while loop here
    }
    else if (x == arg2) {
        execute different version of while loop here
    }
    else..
}

OR

void function(x) {
    while (valid) {
        if (x == arg1) {
            logic here..
        }
        else if (x == arg2) {
            logic here..
        }
        else..
    }
}

Or would it be better to segment the while loops into functions?

expl0it3r
  • 325
  • 1
  • 7
  • 1
    You can try [https://godbolt.org/](https://godbolt.org/) and have an idea. I don't think there is an answer to your question though. It depends on the logic, the input data, the frequency of the input elements, the compiler optimisations etc. – Gerardo Zinno Aug 01 '20 at 18:18
  • 4
    I believe it should not matter for most modern compilers with proper optimization flags and non complete tasks. But I prefer first version or the separate function version. – Mohit Jain Aug 01 '20 at 18:18
  • I expect it won't matter unless you call `function()` tens of thousands of times. Then it may matter depending on compiler optimization. And even in the case where the optimizer does not optimize branch prediction for your CPU may optimize it for you anyways. – drescherjm Aug 01 '20 at 18:21
  • 3
    Wrong question, instead ask which is a more straightforward way to arrange the code. Leave the optimization to the compiler, unless and until you can proove that the compiler needs some help. – john Aug 01 '20 at 18:22
  • 1
    Measure it! Everything else is cargo cult programming. – bitmask Aug 01 '20 at 18:24
  • 1
    Source code is a description of observable behaviour, not a list of instructions to perform. The compiler provides the list of instructions, and anything that the compiler can do to your code to make it faster without breaking the described behaviour, [it will do](https://stackoverflow.com/questions/15718262/what-exactly-is-the-as-if-rule). – user4581301 Aug 01 '20 at 18:31

2 Answers2

4

We live in wonderful times where you can answer this yourself, always, without asking anyone.

But first of all: Accept that you're doing micro-optimizations. It really matters what exactly appears in the tests, how the values are accessed in terms of cache locality, and what's done if a test succeeds. It is a valid result that such micro-optimizations make no difference. It simply tells you that you're focusing at the wrong area.

It also tells you that the billions of dollars worth of R&D done by the CPU manufacturer is paying off. Modern CPUs are quite amazing at doing everything possible to get performance out of the code. If you're not doing something ridiculously bad, like having std::list<type*>, and not wasting memory for the items you test, you'll be in a good starting point already.

  1. Look at the optimized assembly output on godbolt (or from your own run of the compiler!). Make sure that you did indeed enable optimization (-O3 for gcc/clang, /O4 for msvc). Otherwise you'd be wasting time. Pay special attention to compiler's attempts at vectorizing the loop.

  2. Run benchmarks. But understand what makes for good benchmarking. See eg How can I benchmark the performance of C++ code?

  3. Make your question more specific. As it stands, without you showing real code (properly minimized, though!), it can't be reasonably answered and should be closed.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

The first version tests x == argN once, while the second one does it in every loop. The first is better with respect to given pseudo code.

Łukasz Ślusarczyk
  • 1,775
  • 11
  • 20
  • 3
    Note that a good compiler will spot this and if the `if` can be [hoisted](https://en.wikipedia.org/wiki/Loop-invariant_code_motion) without causing harm, it will hoist. – user4581301 Aug 01 '20 at 18:24