Being a Programmer, one of the most important task is to analyse the code and optimize it , so that code should use less memory and time
Being a Programmer, one of the most important task is to analyse the code and optimize it , so that code should use less memory and time. One may think, finding a part of code which is taking up more space in memory is tedious task. But wait, fortunately in Python, we have different libraries for performance analysis, example: cProfile, line-profiler, guppy, memory-profiler. In this article, we’ll discuss memory-profiler library for monitoring memory consumption.
Module memory-profiler returns line-by-line analysis of memory consumption for python programs.
pip install -U memory_profiler
To get the line-by-line analysis follow below two steps:
In below example, we are calling my_func(), in which we are just did some calculation and stored in variable ‘a’ , and after that we deleted ‘a’. This program may not be related to real-world problem, but this code is enough to understand memory profiling.
from memory_profiler import profile @profile def my_func(x): a = [x]*(10**8) del a my_func(20)
Line ## Mem usage Increment Occurences Line Contents ============================================================ 2 20.3 MiB 20.3 MiB 1 @profile 3 def my_func(x): 4 783.2 MiB 762.9 MiB 1 a = [x]*(10**8) 5 20.3 MiB -762.9 MiB 1 del a
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.
Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.
Performance is probably not the first thing that pops up in your mind when you think about Python. Nor is it required in a typical I/O intensive application, where most CPU cycles are spent waiting. But if a few small fixes can give your program those tiny performance boosts, that doesn't hurt, right?
In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.
Performance is an integral part of the Application design and plays a vital role in the success of your product/application.