How to make your program to be fast?
1. Use Pre Increment or Pre Decrement in place of Post Increment or Post Decrement operator. i.e. use ++i or –i in place of i++ or i–.
Because ++i operator take increment and save it i.e. One CPU Instruction is required for it. and in case of i++ first save the value of i in register and then Increment, i.e. It takes two instruction set.
So my dear just imagine if you have a loop of 10000 . Then you can save 10000 CPU instruction to be wasted.
PRACTICE: Do practice to write simple loops as for( i=0; i
in place offor( i=0;i
2. Use either for( i=n; i>=0;–i) or for( i=0;i< =n;++i)
Depends on CPU which are you using. Just check CPU Ticks in both cases which for loop is taking more time. mostly CPU give more efficiently on
3. Never use function call inside for loop. Make it redesign to be a for loop inside one function.
Example: In place of
Because No Overhead of creating stack frame, saving register etc in every loop and don’t declare a variable in a loop and avoid to put any expression in a loop If that expression can be without loop with same output.
4. Don’t Use Math library function pow(num1,num2) if num1 is multiple of 2. Because POW() library function is very costly. You can imagine about this arithmetic operation.
If num1==2 then use ” Left shift Operator” num1< num2 . It is 100 times fast that Pow() library Function.
5. Always Declare any multi dimensions Array with the power Of 2.
example int arr in place of int arr etc
int arr in place of int arr etc.