There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

AVincentInSpace , (edited )

god forbid anyone do multiple non-nested if blocks one after another in the same function as the function does multiple different things or (horror of horrors) put an if-else inside a loop

also unless your compiler is completely and utterly brain dead (as is the case with Python, Java, C#, and most other languages that only pretend to be compiled compile to bytecode) a switch and a series of elif statements will compile to the exact same sequence of machine instructions. you can check on godbolt.org if you don’t believe me.

modern compilers are insanely smart. as an example, this loop counts the number of 1’s in the binary representation of a number:


<span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">while </span><span style="color:#323232;">(n)
</span><span style="color:#323232;">    {
</span><span style="color:#323232;">        n </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> n </span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;">amp; (n </span><span style="font-weight:bold;color:#a71d5d;">- </span><span style="color:#0086b3;">1</span><span style="color:#323232;">);    </span><span style="font-style:italic;color:#969896;">// clear the least significant bit set
</span><span style="color:#323232;">        count</span><span style="font-weight:bold;color:#a71d5d;">++</span><span style="color:#323232;">;
</span><span style="color:#323232;">    }
</span>

LLVM will recognize that this is what you are trying to do and emit a single POPCNT instruction on x86, eliminating the loop entirely.

also how would you even use if statements for arithmetic at all? you aren’t thinking of that one joke isEven() function, are you?

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines