How to perform code optimization
Before directly jump into how to perform code optimization let’s understand what is code optimization.
What is code optimization?
For understanding code optimization let’s first understand the meaning of optimization, optimization means to make things more usable, more effective. Code optimization is to write a code that uses less memory, less CPU power, and speed up the processing time, but make sure that the output of that program must not change.
Why code optimization is so valuable?
Whenever we write code or a program for simple things like I used to write in my college time i.e. creating a calculator or creating simple gaming applications, at that time we are not actually worried about the compilation time or processing speed, but when we write a code for an organization or a company it matters a lot.
If we do proper code optimization, it will help us to improve performance and reduce the consumption of resources, and we can assign unused resources to other things, resources can be memory or CPU power or anything. So it is also useful for load balancing and stuff like that.
What are the prerequisites for code optimization?
For performing code optimization for your source code, you need a thorough knowledge of your technology. And you are good to go with it.
How to do code optimization?
After understanding what is code optimization and why we use it now, the main question how we can do it. So let’s understand how we can do code optimization in our own code with its different types:
In machine-independent code optimization, we remove the variables or part of the code which do not require any CPU power or memory.
Let’s understand this with an example,
Code without optimization:
for x in range(6): i = 9 print(x + i)
In this code, variable ‘i’ will initializes 6 times, which is inappropriate.
Code with optimization:
i = 9 for x in range(6) print(x + i)
In this code, we initialize the variable ‘i’ outside of the loop so now it will initialize only for a single time, so this is how we can perform machine-independent code optimization.
2) Machine dependent:
In machine dependent optimization the optimization done after the target code has been produced. Machine dependent optimization can further be divided into different types.
i) Dead-code Elimination:
Dead-code Elimination is, removes the part of the code which is no longer in use for our program or the part of the code which is not executable or the code whose output is not usable.
ii) Partially dead code:
Partially dead code is the code which is going to run on certain conditions, and we know that the part of that code is never going to come on that condition, so in that case, we should remove that part of the code.
Let’s understand this concept with a simple example,
a = 9 b = 9 if a == b: print("yes") else print("no")
In the above example, we have already defim=ne the value of a and b, which are the same. so it will always be going into if condition and ignores the else condition.
So for these type of codes, we can write the code like this,
a = 9 b = 9 if a == b: print("yes")
iii) Partial Redundant code:
The code which is partially redundant and of not use, we should remove that. for example,
if a == b: c = a + b else: c = a - b c = a + b
here, if the value of a and b are going to remain the same for the whole program. After condition executes we are going to assign c = a + b, so in that case, we can remove this part of the code. So first we have to make sure that the value of a and b will be unchanged.
Code optimization can be done for different conditions in different types and with the help of code optimization we can increase performance, load balancing, memory wastage and decrease CPU power consumption. Hence, performing code optimization in your program or code is highly recommended!
Thank you for reading !
Happy coding 😊