-1

I guess this situation came across in every programmer,where we can use comparison operator '==',in my case situation is like this,a c++ pgm

code 1:This has been used in all files except constructor

if(a==10)
{
 //do something;
}

but i can do the same as above with the following way, i set a bool variable to true when variable a becomes 10 in constructor itself,i.e

constructor_name()
{
 boolean variable_name=TRUE;//when a == 10;
}

then i use the following code in my all files instead of code 1,

code 3:

if(variable_name)
{
 //do same as first code
}

which is better for performance ,the code 1 or code 3.I hope i have illustrated my situation so than you can understand.Please help me.Thanks in advance.

Anil
  • 1,748
  • 8
  • 32
  • 67
  • 1
    Depends on a lot of factors, but looks like a micro-optimization to me. [Is micro-optimization worth the time?](http://stackoverflow.com/questions/3470990/is-micro-optimization-worth-the-time) – Alexander O'Mara May 31 '16 at 04:15
  • 1
    I highly doubt that you're going to get any difference in performance. Simply write the clearest code and let the compiler's optimizer do its job. – PaulMcKenzie May 31 '16 at 04:19
  • Skipping the micro-optimization question, there are two big unknowns here. How often are these objects created? How often is this if statement evaluated? – Michael Albers May 31 '16 at 04:20
  • let us consider ,the if statement is evaluated in every functions,i just gave a general thing,mainly that count will make a difference in performance. – Anil May 31 '16 at 04:25
  • Adding a second variable introduces a maintenance burden for, likely, an unmeasurable gain. – Galik May 31 '16 at 05:01

2 Answers2

2

You shouldn't micro-optimize. You will hardly notice any difference between your 2 version in performance (maybe you will save 1 CPU cycle), but it is not worth the time and effort, especially because nowadays CPUs are really fast.

Only optimize if you profile and find a bottleneck in your code.

Look at it this way, if you store the boolean variable in the class, it uses memory (1 byte) for maybe saving 1 CPU cycle. Depending on how often you create the class, that can scale up (even though the amount would still be ridiculously small). You maybe saved 1 cycle, but you lost 1 byte.

If you wrote this in production code, I am sure that others would find it confusing (I would), and wonder why you put a isTen boolean in the class, instead of just comparing the value using operator==.

Also, there may be a bug if you change a outisde of the constructor to 10, then isTen would still be false, but a is 10!

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
-1

I found the below thing would make a difference,

consider variable a is an integer variable and it takes 4 bytes(assuming 4bytes for int),then compiler has to perform comparision of 4 bytes of memory where as a bool variable takes 1byte ,i guess this makes a differance in performance.

Anil
  • 1,748
  • 8
  • 32
  • 67
  • A 4 byte integer would just be a 32bit word, which can be compared in one instruction in a 32bit system. A bool may even be implemented as a full word as well, the standard only says that it must be "at least" a byte. – Ramon May 31 '16 at 06:57