Some Simple Code Optimization Techniques in C & C++

Sometimes after designing your hardware, you might face some problems in your project. For instance, the application did not run as fast as you wanted, to react to inputs. Alternatively, over-flow happened during the execution of your code, etc. There are just some problems we might face.

So, let us take a quick look at some principle techniques used in code optimization progress.

What you could do? Designing the hardware again?! NO, I do not think it was a good idea. The first solution I am thinking about is refactoring/optimizing the code.

The code can be optimized in some ways or some aspects. The most important thing in optimizing the code is identifying the right part of it. So, you must consider it by spending time as much as possible.

I have some rules on the manner I want to optimize:

1.     The stability of code is the most important thing and you must assure that nothing can affect it.

2.     Just optimize the parts of code that really is essential.

3.     After changing the code, you must check the stability of it.

 

As I mentioned before optimizing the code can be done in some aspects, so you must know enough and also do practice as much as you need to get the appropriate level of experience. Let us look at the popular aspects.

 

-         Memory

-         Function

-         Loops & Statements

-         Algorithm

-         Architecture

To optimize the code you must know enough about the concepts of the programming language which you code with and about the operating system, hardware and other things that are important and can affect the performance

Memory & Function

The best way to reduce the amount of memory which is allocated in the stack by calling the function is by passing the parameters by reference. It means that you must send pointers as input arguments leading to increase the speed of running the function. This is because when you send parameters by reference no more memory will allocate in the stack and the function access to the original copy and no more operations is required.

The other solution is reducing the local variables as much as possible. Because as mentioned before allocating the memory reduce the speed of executing of methods.

 

void calculate(int a, int b)

{

}

 

void calculate(int *a, int *b)

{

}

 

Loops & Statements

Suppose you have a nested loop on a multi-dimension array. It matters that how you iterate on the array. Let us look at the code below:

int  my_var[200][200];

int res[200];

 

for(int col=0;col<200;col++)

    for(int row=0; row<200; row++)

          res[col]+=my_var[row][col];

 

for(int row=0;row<200;row++)

    for(int col=0; col<200; col++)

          res[row]+=my_var[row][col];

 

The second code runs faster. This is because the my_var variables in the memory are allocated sequentially. The first row of my_var is the first part, next of it is the second row and …. It means that when you iterate on row rather than columns the processor does not require to fetch in every iterate. This is because, when data is fetched to cache in sequential, the probability of existence the next cell in the cache is more than a cell from another row.

 

Conclusion

Those are just some simple example of simple tricks. To optimize the code we require a comprehensive understanding of the programing language we use. Besides, we require to know how we can use the programming language facilities to implement what we want.

 

Please put your comment below and share your idea.

 

To view or add a comment, sign in

More articles by Mohammad Hossein Heydarchi

Insights from the community

Others also viewed

Explore topics