Python Forum

Full Version: How to get memory usage and execution time of each line in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I have below code, and I want to get(calculate) the memory usage and execution time of each line of my code. My code is below, kindly someone help,

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, cbar_kws={'ticks': [0, 2, 4, 6, 8, 10]}, vmin=0, vmax=10) 
plt.show()

 
#Create a DataFrame
d = {'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David','Andrew','Ajay','Teresa'],
   'Score1':[62,47,55,74,31,77,85,63,42,32,71,57],
   'Score2':[89,87,67,55,47,72,76,79,44,92,99,69]}
 
 
df = pd.DataFrame(d)
col_mean=df.mean()
col_std=df.std()
get_disc=df.describe()
For memory usage there is built-in gc - Garbage Collector interface. With gc module one can directly interact with the garbage collector and figure out what objects it's tracking as references and how many of those objects there are.

Starting from Python 3.4 there is also built-in tracemalloc - Trace memory allocations which makes possible to connect an object back to where it was allocated.

For performance measurement there are profilers.
I go through those stuff, due to I am very new to python, I could not understand how to arrange my code. However I do the following, but it does not give any memory usage summary

I refer to: https://pypi.org/project/memory-profiler/

from memory_profiler import profile
@profile
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, cbar_kws={'ticks': [0, 2, 4, 6, 8, 10]}, vmin=0, vmax=10) 
plt.show()
 
  
#Create a DataFrame
d = {'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David','Andrew','Ajay','Teresa'],
   'Score1':[62,47,55,74,31,77,85,63,42,32,71,57],
   'Score2':[89,87,67,55,47,72,76,79,44,92,99,69]}
  
  
df = pd.DataFrame(d)
col_mean=df.mean()
col_std=df.std()
get_disc=df.describe()