tips to optimize the code

tips to optimize the code

Recently I did an assignment on code optimization in c and c++ programming language and believe me I found some interesting results on code optimization.Code optimization is basically a practice to improve your code with respect to various parameters like space,memory access-direct and indirect,time,processor and compiler.Most of he result are taken from google search,youtube videos(specially in case of data sructures ) and Ebooks which I read code optimization in C,C++ and algorithms.So here are some of the best results which I came across and I hope these 20 tips will help you all to code better.


1.Code for correctness first, then optimize it !!!-Offcourse what’s the use of optimizing a wrong code.

 2.Spend at least twice as long  time to optimize the code as you spend to write the code !!! -Optimizing the code is more important than to write the code.

 3.Jumps/branches are expensive. Minimize their use whenever possible !!!-Example- Long if…else if…else if…else if… chains require lots of jumps for cases near the end of the chain (in addition to testing each condition). If possible, convert to a switch statement, which the compiler some- times optimizes into a table lookup with a single jump. If a switch statement is not possible, put the most common clauses at the beginning of the if chain.

 4.Think about the order of array indices !!!-Example-(for C/C++ arrays) array[i][j] and array[i][j+1] are adjacent to each other, whereas array[i][j] and array[i+1][j] may be arbitrarily far apart.So array[i][j+1] fetching rate is less than array[i+1][j] as first most probably is in cache and 2nd one in the main memory.

 5.Prefer iteration over recursion !!!-Recursion involves memory allocation to data variables again and again and  jumping  so consumes more time in execution.

 6.Use inline functions for short functions to eliminate function overhead !!!

 7.Avoid/reduce the number of local variables !!!- Local variables are normally stored on the stack. However if there are few enough, they can instead be stored in registers. In this case, the function not only gets the benefit of the faster memory access of data stored in registers, but the function avoids the overhead of setting up a stack frame.

 8.Reduce the number of function parameters !!!-Same reason  as tip no. 7.

9.Pass structures by reference, not by value !!!

10.If you do not need a return value from a function, do not define one !!!

 11.Try to avoid casting where possible !!!- (Specially for int and float as Integer and floating point instructions often operate on different registers, so a cast requires a copy.

 12.Be very careful when declaring C++ object variables !!!-Initialisation as A a(red) faster than A a; a=red.(where A is class and a is object)

13.Make default class constructors as lightweight as possible !!!-As These default constructors are often called behind your back, where you are not expecting it.

 14.Use shift operations >> and << instead of integer multiplication and division, where possible !!!

 15.For most classes, use the operators += , -= , *= , and /= , instead of the operators + , – , * , and / but For basic data types, use the operators + , – , * , and / instead of the operators += , -= , *= , and /= !!!

 16.Delay declaring local variables !!!- Declaring object variable always involves a function call (to the constructor) so  If a variable is only needed sometimes (e.g., inside an if statement) only declare when necessary, so the constructor is only called if the variable will be used.

 17.For objects, use the prefix operator (++obj) instead of the postfix operator (obj++) !!!- A copy of the object must be made with the postfix operator (which thus involves an extra call the the constructor and destructor), whereas the prefix operator does not need a temporary copy.

18.Avoid unnecessary data initialization and choose data type carefully !!!- If you must initialize a large chunk of memory, consider using memset(),If vaule doesn’t include negative values then choose unsigned instead of int.

 19.Try to do early loop termination and early function returns whenever possible !!!

 20.Consider ways of rephrasing your math to eliminate expensive operations !!!-like ->consider if you can compute values in a loop incrementally (instead of computing from scratch each iteration).

So these are some of the best practices o check the optimize the code but there are plenty more so if you think some of my observation wrong or I missed something important then you are more than welcome to notify me via your comments.

Leave a Reply

Post atricle in article minimum 300 words.