-2

I'm writing an application for iPhone that involves checking a lot of variables. Keeping efficiency in mind, I'd like to know the relative load each kind of if statement takes. The ones I am concerned with are:

if (Variable == 0);

if (Variable != 0);

if (Variable > 0);

if (Variable < 0);

if (Variable <= 0);

if (Variable >= 0);

Basically, what I'm asking is how much load on the processor and memory does each of the above if statements take. Are they truly equal, or are some more processor heavy than the others? Thanks so much!

Nerdatope
  • 127
  • 4
  • 1
    All of those should be the same. But why don't you run your own tests and verify it yourself? – rmaddy Jun 05 '14 at 01:05
  • Thats what I was thinking, I'm just too inexperienced to know how to test such minute changes. Thanks! – Nerdatope Jun 05 '14 at 01:06
  • 1
    even if you write `if (a < 0)`, compiler still may change it to `if (0 > a)` (with different binary been generated). (unless it is C++/ObjC++ with overloaded operator) – Bryan Chen Jun 05 '14 at 01:30
  • @Chuck: Because they add up when you use as many as I do, so while 1 if statement isn't heavy, many have the potential to be. – Nerdatope Jun 05 '14 at 01:59
  • 6
    Then benchmark it with the number of comparisons you are interested in. But I can tell you as somebody who is *not* inexperienced that this is not the sort of thing most programmers would generally look at first, second or even tenth when trying to optimize an algorithm that is too slow. This is what's called a micro-optimization. When you have slow code, look at the slowest part and try to make that faster. This will almost never made a measurable difference. And adjusting your algorithm to do fewer computations will generally yield better results than micro-optimizing a slow algorithm. – Chuck Jun 05 '14 at 02:29

2 Answers2

3

The variable you are going to compare matters much more than the operator you are using. In your question you are using integers. Integers comparisons are fast enough to forget about benchmarking it unless you do an awful lot of comparisons.

If you use an object then you depend on the operator implementation of that object. It will require more cpu cycles but not enough to worry about.

Premature optimization is the root of all evil. Code first and optimize after!

ForguesR
  • 3,558
  • 1
  • 17
  • 39
-3

I would suggest testing each of these calls individually using NSDate. I admit I don't know if it's the best way to do it, but it's pretty simple. You could even wrap each call in it's own method if you wanted. Here is a similar question that should guide you:

How to log a method's execution time exactly in milliseconds?

Community
  • 1
  • 1
joelg
  • 1,094
  • 9
  • 19
  • Wow that worked really well! Here are the times I got: (== 0.000013) (!= 0.000011) (> 0.000010) (< 0.000009) (<= 0.000011) (>= 0.000013) – Nerdatope Jun 05 '14 at 01:29
  • 3
    `mach_absolute_time` should be used instead of `NSDate` for performance profiling. – Bryan Chen Jun 05 '14 at 01:32