1

This is something simply to ease my curiosity, if someone would feel like answering it though that would be fantastic.

With if statements, is the time taken to calculate the result affected by the way it's written?

So what I mean is (if that wasn't overly clear) would the following two statements take the same amount of time to process?

if 1 < 2 and 3 = 3 then
   //do something
end if

compared to

if 1 < 2 then
    if 3 = 3 then
      //Do something
    end if
end if
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Lift
  • 546
  • 2
  • 4
  • 24

2 Answers2

4

If we consider that the compiler will not optimize these two calls, then the second statement will require two branching instructions instead of one. And branching requires some extra work for the CPU because of pipelining. So, technically, the second version will require more work, but it should not matter here.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
3

This is just another case of premature-optimization. You are not going to gain anything by thinking a lot about this.

What you should be focusing on is how to make your code more readable.

Community
  • 1
  • 1
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142