Importance of Optimization.

Importance of Optimization.

The purpose of Optimization is to achieve the Best solution for any given problem. In simple words optimization can be used in Manufacturing plants to figure out how to best run their machinery, to buy raw materials, and also airlines and other passenger transportation services use optimization to determine their schedules.

Hence optimization plays a vital role in almost every sectors.

Lets have a look on Code Optimization!.

Often, when we define it, we assume that our code will perform better by writing or rewriting code several times so that program uses least memory or disk space, minimizes its CPU time to optimize the simple program.

Think of optimizing code, is similar to purchasing a faster car. As a result, our code executes more rapidly, our program consumes less memory than before. Optimization process requires less time to execute in an optimized manner.

Characteristics of good quality code :

To make your program perform better, your code should consists of several qualities to be in optimized state. It should be :

No alt text provided for this image
  • Efficient
  • Readable
  • Maintainability
  • Portable
  • Robustness
  • Reliable

Ok, so basically you have seen the qualities of a good program. These are some advice to make your code simpler.

  1. Design it so that it would be simple to explain to a non-programmer.
  2. Write a test that fits the expectations produced by that design.
  3. Write your code so that non-programmer could easily glean the design from it.

What needs to be optimize?

You can only know what makes your program slow after your first execution of the program till the code gives you the correct results, then running it to see if the correct output of the program is slow. When found to be slow, profiling can show what parts of the program are consuming most of the time. A comprehensive but quick-to-run test suite can then ensure that future optimizations don't change the correctness of your program. In simple words :

  1. Get it right.
  2. Test it is right.
  3. Profile if slow.
  4. Then Optimize.
  5. Repeat from step 2.

Optimizing the code leads to good programming style which should be learned as you learn the language either C, C++, Java, Python, etc.

When to optimize your code?.

Suppose you have implemented your algorithm, and also you have proven its output to be correct, congratulations! You have the correct output on your screen.

But, do you think your code is running fast and using less CPU time and space.

So, this is when you need to optimize your code.

Did you know? The ultimate universal optimization trick, applies in all cases:
- Draw less stuff.
- Update less stuff.

An important thing to keep in mind when looking for ways to optimize your code is that there will probably always be some trade-offs to accept. For example, its either a faster running piece of code or simpler one. And simplicity here does not mean just "code that looks less cool!". Simpler code means its going to be easier to maintain and test.

4-ways to Optimize Python Code :

No alt text provided for this image

Optimizing any code, first you need to understand that what is happening behind the scenes and how it will work. However, there are some situations where you definitely need to give your code a little energy and boost.

Here are few points or examples of my code snippets to understand more clearly about the optimization in python programs.

1. Using List Comprehensions :

In python, writing code for creating list is using list comprehension instead of using traditional method loop. In this example, it is creating binary number representation where all negative numbers will be assigned as 0 and the rest will be assigned as 1.

So, instead of this :

No alt text provided for this image
No alt text provided for this image

You can do :

No alt text provided for this image

You can also try and compare which implementation runs faster using timeit module.

2. Avoid for loops and lists comprehensions where possible :

In the previous example, if you were creating a vector with just one initialization value, instead of using inefficient for-loops and even list comprehensions, you could do something like this :

No alt text provided for this image

3. Avoid unnecessary functions :

Function calls is an approach that can help shave off a significant amount of runtime complexity but it requires a lot of careful thought in terms pf trade-offs. While you want the nice abstraction, extensibility, and re-usability that functions provide, you might not want to have a function for every single thing, that's because function calls are quite expensive in Python. So, sometimes you might need to write getter or a setter methods. On the other hand, some functions makes the code less testable. So final decision really depends on the type of your application.

4. Use built-ins where possible :

Another way of optimizing is using the built-in function s like max(), sum(), map(), reduce(). etc. rather than solving those computations yourself. It follows the rule - "Make it simple".

Click here to know more about built-in functions.

No alt text provided for this image

There are many more such techniques to make your code optimized. Whatever technique we choose to go along with, there’s a rule of thumb that every code optimization endeavor needs to maintain the same objective or output.

You should take care of carrying out the optimization in a such a way that does not change the meaning of the code.

The benefits of code optimization grows in increasing line with the growth of our project, and as even initially small projects can become large with time, acquiring solid code optimization skills almost always have measurable positive results.

Thank you!

Dipali Goyal

Software Engineer II at JPMorgan Chase & Co.

3y

Good work Nargis👏

Saurabh Sinha

Product Engineer at Mbb Labs

3y

Very useful great work Sayyed Nargis Fatima 👏

To view or add a comment, sign in

More articles by Sayyed Nargis Fatima

  • Git v/s GitHub

    Wait! Git and GitHub are different?! Let us see how!..

    10 Comments

Insights from the community

Others also viewed

Explore topics