Python

How much memory does my Python code use? Memory Profiler!

Memory Profiler is an open-source module that uses the psutil module, to monitor the memory consumption of Python functions. It performs a line-by-line memory consumption analysis.

Installation:

Install Memory Profiler from PyPl using:

pip install -U memory_profiler

After you have configured memory_profiler, you can use a decorator to track the memory consumption of the function. The @profile decorator can be used before every function that needs to be tracked. This will track the memory consumption line-by-line.

After decorating all the functions with @profile execute the python script with a specific set of arguments.

import yfinance as yf
from memory_profiler import profile

tickers = ['^AORD', '^DJI']

@profile
def isupdown(x):
    if x > 0:
        return ' up '
    else:
        return ' down '

@profile
def GetStock(stocks):
    stock = yf.Ticker(stocks)
    price = stock.info['regularMarketPrice']
    marketopen = stock.info['regularMarketOpen']
    myval = (((price - marketopen)/marketopen))*100
    points = (price - marketopen)
    stock = stocks + ' - Stock opened at ' + str(marketopen) + ' Currently ' + str(price) + isupdown(
        myval) + str(round(myval, 2)) + '% ' + isupdown(myval) + str(round(points)) + ' points'
    return stock


print(GetStock('^AORD'))
print(GetStock('^DJI'))
Filename: get_stocks.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    20     91.6 MiB     91.6 MiB           1   @profile
    21                                         def isupdown(x):
    22     91.6 MiB      0.0 MiB           1       if x > 0:
    23     91.6 MiB      0.0 MiB           1           return ' up '
    24                                             else:
    25                                                 return ' down '


Filename: get_stocks.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    20     91.6 MiB     91.6 MiB           1   @profile
    21                                         def isupdown(x):
    22     91.6 MiB      0.0 MiB           1       if x > 0:
    23     91.6 MiB      0.0 MiB           1           return ' up '
    24                                             else:
    25                                                 return ' down '


Filename: get_stocks.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    28     77.1 MiB     77.1 MiB           1   @profile
    29                                         def GetStock(stocks):
    30     77.3 MiB      0.2 MiB           1       stock = yf.Ticker(stocks)
    31     91.5 MiB     14.3 MiB           1       price = stock.info['regularMarketPrice']
    32     91.5 MiB      0.0 MiB           1       marketopen = stock.info['regularMarketOpen']
    33     91.5 MiB      0.0 MiB           1       myval = (((price - marketopen)/marketopen))*100
    34     91.5 MiB      0.0 MiB           1       points = (price - marketopen)
    35     91.6 MiB      0.1 MiB           7       stock = stocks + ' - Stock opened at ' + str(marketopen) + ' Currently ' + str(price) + isupdown(
    36     91.6 MiB      0.0 MiB           6           myval) + str(round(myval, 2)) + '% ' + isupdown(myval) + str(round(points)) + ' points'
    37     91.6 MiB      0.0 MiB           1       return stock


^AORD - Stock opened at 7356.1 Currently 7427.9 up 0.98%  up 72 points
Filename: get_stocks.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    20     95.9 MiB     95.9 MiB           1   @profile
    21                                         def isupdown(x):
    22     95.9 MiB      0.0 MiB           1       if x > 0:
    23     95.9 MiB      0.0 MiB           1           return ' up '
    24                                             else:
    25                                                 return ' down '


Filename: get_stocks.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    20     95.9 MiB     95.9 MiB           1   @profile
    21                                         def isupdown(x):
    22     95.9 MiB      0.0 MiB           1       if x > 0:
    23     95.9 MiB      0.0 MiB           1           return ' up '
    24                                             else:
    25                                                 return ' down '


Filename: get_stocks.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    28     91.3 MiB     91.3 MiB           1   @profile
    29                                         def GetStock(stocks):
    30     91.3 MiB      0.0 MiB           1       stock = yf.Ticker(stocks)
    31     95.9 MiB      4.5 MiB           1       price = stock.info['regularMarketPrice']
    32     95.9 MiB      0.0 MiB           1       marketopen = stock.info['regularMarketOpen']
    33     95.9 MiB      0.0 MiB           1       myval = (((price - marketopen)/marketopen))*100
    34     95.9 MiB      0.0 MiB           1       points = (price - marketopen)
    35     95.9 MiB      0.0 MiB           7       stock = stocks + ' - Stock opened at ' + str(marketopen) + ' Currently ' + str(price) + isupdown(
    36     95.9 MiB      0.0 MiB           6           myval) + str(round(myval, 2)) + '% ' + isupdown(myval) + str(round(points)) + ' points'
    37     95.9 MiB      0.0 MiB           1       return stock


^DJI - Stock opened at 32989.27 Currently 33544.34 up 1.68%  up 555 points
Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.