It’s not hard to fix the problem once you know where the problem is.

Nobody writes the best code in the first instance of building logic. Especially in the startup environment, implementing the functionality is what we call coding. We don’t get much time to follow any standards of coding. All we do is write functions or classes and giving whatever names we feel apt for those.

Maintaining coding standards can be time-consuming at times, but the least we can do is making sure our code is efficient — not taking much time or memory for execution. As we have good compute power most of the time, we neglect this part unless we get OOM errors or have to wait minutes for getting the output.

Do you really need optimization?

Every code needs optimization and any functionality should be implemented in the optimal way possible -Ideally. Time and memory both are linked to money. So if your code takes more time or more memory, it is taking extra money out of your pocket. But we don’t bother about that much as most of our code serves a limited purpose and the money factor doesn’t come into picture.

I am a data scientist and I write code almost every day. For most of the part, I don’t worry about optimization while writing the code. I will analyze my code once I finished the implementation using the profiler and decide whether it needs optimization or not.

If a particular block of my code takes most of the time, then I will try to optimize that block. But if the time taken is spread across the whole code, I don’t bother improving it further.

How do we know which parts of the code needs optimization?

That’s where profiling comes into play. There are many profilers available for both time and memory. We will save memory profiling for some other day. Let’s concentrate on time profiling for now. cProfile is one of the ways of time profiling. Let’s work through an example.

#programming #profiling #cprofile #python #optimization #profiling and optimizing your python code

Profiling and Optimizing your Python Code
1.45 GEEK