Basic Code Optimizations in C
Generally, resources are shared between different processes. Suppose your program takes more resources, then definitely it will affect the performance of other processes that need the same resources. So we have a need to write and optimize our program keeping in mind resources e.g. processor’s time and main memory.
Following are some Optimization techniques
- Optimizations in loop
- Unroll small loops: Most of the times Compiler does this automatically, but it is a good habit of writing optimized codes. Matrix updations using this is very advantageous.
Program 1:
#include <stdio.h>intmain(void){intfact[5];fact[0] = 1;// Overhead of managing a counter// just for 4 iterations// is not a good ideafor(inti = 1; i < 5; ++i) {fact[i] = fact[i - 1] * i;}return0;}chevron_rightfilter_noneProgram 2:
#include <stdio.h>intmain(void){intfact[5] = { 1, 1, 2, 6, 24 };// Here the same work is done// without counter overheadreturn0;}chevron_rightfilter_none - Avoid calculations in loop: We should avoid any calculation which is more or less constant in value. Inner loops should have minimum possible calculations.
Program 1:#include <stdio.h>intmain(void){intarr[1000];inta = 1, b = 5, c = 25, d = 7;// Calculating a constant expression// for each iteration is not good.for(inti = 0; i < 1000; ++i) {arr[i] = (((c % d) * a / b) % d) * i;}return0;}chevron_rightfilter_noneProgram 2:
#include <stdio.h>intmain(void){intarr[1000];inta = 1, b = 5, c = 25, d = 7;// pre calculating the constant expressioninttemp = (((c % d) * a / b) % d);for(inti = 0; i < 1000; ++i) {arr[i] = temp * i;}return0;}chevron_rightfilter_none - Avoid pointer Dereference in loop: Pointer dereferencing creates lots of trouble in memory. So better assign it to some temporary variable and then use that temporary variable in the loop.
Program 1:#include <stdio.h>intmain(void){inta = 0;int* iptr = &a;// Dereferencing pointer inside loop// is costlyfor(inti = 1; i < 11; ++i) {*iptr = *iptr + i;}printf("Value of a : %d", a);return0;}chevron_rightfilter_noneOutput:Value of a : 55
Program 2:
#include <stdio.h>intmain(void){inta = 0;int* iptr = &a;// Dereferencing pointer outside loop// and saving its value in a temp variableinttemp = *iptr;for(inti = 1; i < 11; ++i) {// performing calculations on temp variabletemp = temp + i;}// Updating pointer using final value of temp*iptr = temp;printf("Value of a : %d", a);return0;}chevron_rightfilter_noneOutput:Value of a : 55
- Use Register variables as counters of inner loops: Variables stored in registers can be accessed much faster than variables stored in memory.
Program:#include <stdio.h>intmain(void){registerinti = 0;registerintj = 0;intn = 5;// using register variables// as counters make the loop fasterfor(i = 0; i < n; ++i) {for(j = 0; j <= i; ++j) {printf("* ");}printf("\n");}return0;}chevron_rightfilter_noneOutput:* * * * * * * * * * * * * * *
- Unroll small loops: Most of the times Compiler does this automatically, but it is a good habit of writing optimized codes. Matrix updations using this is very advantageous.
- Fast Mathematics
- Avoid unnecessary Integer Division: Division operations are slow so we should minimize the division operations.
Program:#include <stdio.h>intmain(void){inta = 100, b = 2, c = 5;// int d=a/b/c; two division operatorsintd = a / (b * c);// single division operatorreturn0;}chevron_rightfilter_none - Multiplication and division by power of 2: Use left shift(<<) for multiplication and right shift(>>) for division. The bit operations will be much faster than multiplication and division operations.For simple operations, the compiler may automatically optimize the code but in case of complex expressions it is always advised to use bit operations.
Example :
Multiply by 6 : a= a<<1 + a<<2; Multiply by 7 : a= a<<3 - a; Divide by 8 : a= a>>3; // division by power of 2
- Simplifying Expressions: Sometimes we can reduce some operations by simplifying expressions.
Example :a*b + a*b*c + a*b*c*d ---> (a*b)*(1 + c*(1 + d)) L.H.S can be Simplified to R.H.S L.H.S : 6 multiplications and 2 additions R.H.S : 3 multiplications and 2 additions
- Avoid unnecessary Integer Division: Division operations are slow so we should minimize the division operations.
- Optimization with Switch Statement
Compilers translate switch statements in different ways. If case labels are small contiguous integer values, then it creates a jump table. This is very fast and doesn’t depend on the number of case labels also. If case labels are longer and not contiguous then it creates comparison tree i.e. if…else statements. So, in this case, we should keep the most frequent label first and the least frequent label should be at last.Sometimes we see a lot of repeated code written in all cases except one or two statements
Example :
switch(expression) { case a: ........ ........ break; case b: ........ ........ break; case c: common statements; different statements; common statements; break; case d: common statements; different statements; common statements; break; ' case e: common statements; different statements; common statements; break; case f: common statements; different statements; common statements; break; default: break; }We can take all cases together and can write common statements only once and different statements in related cases using another switch. Here we will take cases c, d, e, f together and write common statements, then we can use another switch and write different statements in case c, d, e, f. Then after this switch, we can again write common statements.
switch(expression) { case a: ........ ........ break; case b: ........ ........ break; case c: case d: case e: case f: common statements; switch(expression); { case c: different statements; break; case d: different statements; break; case e: different statements; break; case f: different statements; break; } /*End of switch*/ common statements; break; default: break; }/*End of switch*/ - Prefer int to char or short
We should always prefer int to char because C performs all operations of char with an integer. In all operations like passing char to a function or an arithmetic operation, first char will be converted into integer and after compilation of operation, it will again be converted into char. For a single char, this may not affect the efficiency but suppose the same operation is performed 100 times in a loop then it can decrease the efficiency of the program. - Prefer pre Increment/Decrement to post Increment/Decrement
In pre-increment, it first increments the value and just copies the value to variable location but in post-increment, it first copies the value to a temporary variable, increments it and then copies the value to the variable location. If post-increment is 1000 times in a loop it will decrement the efficiency. - Order of Expression Evaluation
-
A || B
Here first A will be evaluated, if it’s true then there is no need of evaluation of expression B. So We should prefer to have an expression which evaluates to true most of the times, at the A’s place.
-
A && B
Here first A will be evaluated, if it’s false then there is no need of evaluation of expression B. So We should prefer to have an expression which evaluates to false most of the times, at the A’s place.
-
Recommended Posts:
- Basic Input and Output in C
- Basic Input / Output in C++
- How to print range of basic data types without any library function and constant in C?
- Self Destructing Code in C
- Writing OS Independent Code in C/C++
- Convert C/C++ code to assembly language
- Code to generate the map of India (with explanation)
- Printing source code of a C program itself
- How to write a running C code without main()?
- Code valid in both C and C++ but produce different output
- Convert C/C++ program to Preprocessor code
- C++: Methods of code shortening in competitive programming
- LEX code to extract HTML tags from a file
- Code Optimization Technique (logical AND and logical OR)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


