Home › Forums › C Programming › Tips to make your program more efficient
- This topic has 1 reply, 2 voices, and was last updated 16 years, 8 months ago by deepaksancheti.
- AuthorPosts
- March 10, 2008 at 1:32 am #2073shalizyParticipant
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
=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 on1for( i=n; i>=0;--i)3. Never use function call inside for loop. Make it redesign to be a for loop inside one function.
Example: In place of12345678for( i=n;i<=0;--i)<br />sum = Fun4Add(i);<br /><br />use Fun4Add(int num)<br />{<br />sum+=num;<br />}<br />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[2][1024] in place of int arr[1][1023] etc
int arr[32][128] in place of int arr[30][128] etc.Because Reason is same as i have given in 4.
- March 13, 2008 at 6:36 pm #3346deepaksanchetiParticipant
Very gud tips…………
Now I will try to make use of them…….
Thank you…
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.