Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is faster? a loop or multiple if condition

I was wondering what was faster. Is it when you do a for loop with only one instruction (i.e 1=1) 9 times or is it when you go through 9 if condition?

I think the ifs are faster because you don't need to check for instruction in the loop

like image 554
Chax Avatar asked Nov 13 '15 15:11

Chax


1 Answers

It should be pretty much the same - because for loops are essentially checking if a condition is true and running a block of code - very similar to if statements.

For details on how for loops and if statement are typically implemented in assembly - have a look at https://en.wikibooks.org/wiki/X86_Disassembly/Loops and complex IF statement in assembly

like image 87
TR1 Avatar answered Oct 13 '22 00:10

TR1