6

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

Chax
  • 1,041
  • 2
  • 14
  • 36
  • What are you doing in the loop? The loop is likely to be more readable, which is what you should actually be concerned with, unless you know otherwise (and if you are worried about performance, then LuaJIT will likely be able to unroll the loop). – Colonel Thirty Two Nov 13 '15 at 15:58
  • I wanted to know if it was faster for the computer to look through an array with a for loop or with multiple condition. I know the array is more readable and would be the good way to do it. I want to know what it implies in the computer to use multiple if – Chax Nov 13 '15 at 16:14

1 Answers1

3

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

Community
  • 1
  • 1
TR1
  • 323
  • 1
  • 9
  • Thanks, exactly what i was looking for. – Chax Nov 13 '15 at 16:15
  • Would'nt the for loop be the slowest of the three typoe of loop because it has to increment. I'm speaking nano second here. – Chax Nov 13 '15 at 16:16
  • 1
    Yes if you perform an increment or any other operation - but you can have a for look without an increment - for a Java example - see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html – TR1 Nov 13 '15 at 16:26