Give 2 examples of a code optimization.

Showing Answers 1 - 6 of 6 Answers

I am giving one example of code optimization you can follow for C++ program which is a OOPS programming language.

   exforsys e1;
   if (c1)
     e1 = w;

In the above we have declared e1 before if statement. In C++ t is enough if we declared variable wherever it is needed. So in the above case e1 is used within if alone and so in that scope if it declared the code would be optimized.

So the optimized code of above can be as below:

   if (c1)
    exforsys e1 = w;

So the thumb rule that could be used is to declare variables at the least scope provided the object is used only within that scope.

  Was this answer useful?  Yes

I am giving below another example of code optimization you can follow for C++ program. As we all know prefix operators apply the operations of incrementing or decrementing as specified and the new value is stored. In case of postfix operator the operations of incrementing or decrementing as specified is done and the old value of object is also to be stored. This makes the requirement of a temporary object for storage of the old value of object. So it is better to avoid postfix operators. One may be puzzled then how to use postfix operators if needed. By careful handling of operators one can implement postfix operator using prefix operator itself. By this way the code would be optimized.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions