Just knowing how to write a program will not make you an ace in programming. To overcome both the heads and tails, in order to win your toss is the utmost requirement. The content here provides you to tackle the TIME COMPLEXITY of your program by handling the nucleus of your code. These are as follows:
- Choose a better algorithm.
- Replace sequential search with binary search or hash look-ups.
- Replacing insertion or bubble sort by quick sort, merge sort or radix sort.
- For if(x!=0)x=0; we can fasten it by simply assigning x=0; without checking whether it is already non-zero or not.
- Loop inversion: replace for(i=0;i<5;i++) with for(i=5;i–;)for faster code.
- Strength reduction is also an approach by replacing division by bitwise shift, power of by multiplication, multiplication by addition, etc.
- Replace recursive algorithms with iterative ones
- The most repeated calculations can be replaced by look-up tables i.e. those calculated values are stored at a place and then those are simply used without computing again.
- Avoid referring to global or static variables inside the tightest loops.
- Function calls are often expensive when called inside a most iterative loop.
- Using strlen() as a conditional statement is not advisable instead assign the strlen() to a variable and use it.
- Instead of copying large structures you should copy the pointers instead.
- Inlining a function and using the register memory for faster responses.
- Writing clear and simpler codes.
These were only some of the approaches to optimization and the most important of all is to select a good compiler and use various inbuilt methods to optimize the code. The assembly language optimization is also an approach. But more or less all goes well in the end if the selection of algorithm for a task is made intelligently. Programming is never a monotonous task but it is a field where in order to intensify the performance one has to do the optimization in the best possible way. The optimization in the code reduces hardware complexities and increases efficiency. So people, knowing many programming languages will not make you an efficient programmer but to skillfully program in a couple of them will be the ladder to becoming a star-programmer. So let’s code! By- Viral Soni