{"id":419,"date":"2022-03-16T13:57:28","date_gmt":"2022-03-16T03:57:28","guid":{"rendered":"http:\/\/www.mrmarkyoung.com\/oracle\/?p=419"},"modified":"2022-03-16T14:00:35","modified_gmt":"2022-03-16T04:00:35","slug":"how-much-memory-does-my-python-code-use-memory-profiler","status":"publish","type":"post","link":"http:\/\/www.mrmarkyoung.com\/oracle\/2022\/03\/16\/how-much-memory-does-my-python-code-use-memory-profiler\/","title":{"rendered":"How much memory does my Python code use? Memory Profiler!"},"content":{"rendered":"\n<p>Memory Profiler is an open-source module that uses&nbsp;the <code><strong>psutil<\/strong><\/code><strong>&nbsp;<\/strong>module, to monitor the memory consumption of Python functions. It performs a line-by-line memory consumption analysis.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a53a\">Installation:<\/h2>\n\n\n\n<p id=\"0ff0\">Install Memory Profiler  from PyPl using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark><strong><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color has-black-color\">pip install -U memory_profiler<\/mark><\/strong><\/mark><\/pre>\n\n\n\n<p id=\"2314\">After you have configured memory_profiler, you can use a decorator to track the memory consumption of the function. The&nbsp;<code>@profile<\/code>&nbsp;decorator can be used before every function that needs to be tracked. This will track the memory consumption line-by-line.<\/p>\n\n\n\n<p id=\"f468\">After decorating all the functions with&nbsp;<code>@profile<\/code>&nbsp;execute the python script with a specific set of arguments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yfinance as yf\nfrom memory_profiler import profile\n\ntickers = &#91;'^AORD', '^DJI']\n\n@profile\ndef isupdown(x):\n    if x > 0:\n        return ' up '\n    else:\n        return ' down '\n\n@profile\ndef GetStock(stocks):\n    stock = yf.Ticker(stocks)\n    price = stock.info&#91;'regularMarketPrice']\n    marketopen = stock.info&#91;'regularMarketOpen']\n    myval = (((price - marketopen)\/marketopen))*100\n    points = (price - marketopen)\n    stock = stocks + ' - Stock opened at ' + str(marketopen) + ' Currently ' + str(price) + isupdown(\n        myval) + str(round(myval, 2)) + '% ' + isupdown(myval) + str(round(points)) + ' points'\n    return stock\n\n\nprint(GetStock('^AORD'))\nprint(GetStock('^DJI'))\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Filename: get_stocks.py\n\nLine #    Mem usage    Increment  Occurrences   Line Contents\n=============================================================\n    20     91.6 MiB     91.6 MiB           1   @profile\n    21                                         def isupdown(x):\n    22     91.6 MiB      0.0 MiB           1       if x &gt; 0:\n    23     91.6 MiB      0.0 MiB           1           return ' up '\n    24                                             else:\n    25                                                 return ' down '\n\n\nFilename: get_stocks.py\n\nLine #    Mem usage    Increment  Occurrences   Line Contents\n=============================================================\n    20     91.6 MiB     91.6 MiB           1   @profile\n    21                                         def isupdown(x):\n    22     91.6 MiB      0.0 MiB           1       if x &gt; 0:\n    23     91.6 MiB      0.0 MiB           1           return ' up '\n    24                                             else:\n    25                                                 return ' down '\n\n\nFilename: get_stocks.py\n\nLine #    Mem usage    Increment  Occurrences   Line Contents\n=============================================================\n    28     77.1 MiB     77.1 MiB           1   @profile\n    29                                         def GetStock(stocks):\n    30     77.3 MiB      0.2 MiB           1       stock = yf.Ticker(stocks)\n    31     91.5 MiB     14.3 MiB           1       price = stock.info&#91;'regularMarketPrice']\n    32     91.5 MiB      0.0 MiB           1       marketopen = stock.info&#91;'regularMarketOpen']\n    33     91.5 MiB      0.0 MiB           1       myval = (((price - marketopen)\/marketopen))*100\n    34     91.5 MiB      0.0 MiB           1       points = (price - marketopen)\n    35     91.6 MiB      0.1 MiB           7       stock = stocks + ' - Stock opened at ' + str(marketopen) + ' Currently ' + str(price) + isupdown(\n    36     91.6 MiB      0.0 MiB           6           myval) + str(round(myval, 2)) + '% ' + isupdown(myval) + str(round(points)) + ' points'\n    37     91.6 MiB      0.0 MiB           1       return stock\n\n\n^AORD - Stock opened at 7356.1 Currently 7427.9 up 0.98%  up 72 points\nFilename: get_stocks.py\n\nLine #    Mem usage    Increment  Occurrences   Line Contents\n=============================================================\n    20     95.9 MiB     95.9 MiB           1   @profile\n    21                                         def isupdown(x):\n    22     95.9 MiB      0.0 MiB           1       if x &gt; 0:\n    23     95.9 MiB      0.0 MiB           1           return ' up '\n    24                                             else:\n    25                                                 return ' down '\n\n\nFilename: get_stocks.py\n\nLine #    Mem usage    Increment  Occurrences   Line Contents\n=============================================================\n    20     95.9 MiB     95.9 MiB           1   @profile\n    21                                         def isupdown(x):\n    22     95.9 MiB      0.0 MiB           1       if x &gt; 0:\n    23     95.9 MiB      0.0 MiB           1           return ' up '\n    24                                             else:\n    25                                                 return ' down '\n\n\nFilename: get_stocks.py\n\nLine #    Mem usage    Increment  Occurrences   Line Contents\n=============================================================\n    28     91.3 MiB     91.3 MiB           1   @profile\n    29                                         def GetStock(stocks):\n    30     91.3 MiB      0.0 MiB           1       stock = yf.Ticker(stocks)\n    31     95.9 MiB      4.5 MiB           1       price = stock.info&#91;'regularMarketPrice']\n    32     95.9 MiB      0.0 MiB           1       marketopen = stock.info&#91;'regularMarketOpen']\n    33     95.9 MiB      0.0 MiB           1       myval = (((price - marketopen)\/marketopen))*100\n    34     95.9 MiB      0.0 MiB           1       points = (price - marketopen)\n    35     95.9 MiB      0.0 MiB           7       stock = stocks + ' - Stock opened at ' + str(marketopen) + ' Currently ' + str(price) + isupdown(\n    36     95.9 MiB      0.0 MiB           6           myval) + str(round(myval, 2)) + '% ' + isupdown(myval) + str(round(points)) + ' points'\n    37     95.9 MiB      0.0 MiB           1       return stock\n\n\n^DJI - Stock opened at 32989.27 Currently 33544.34 up 1.68%  up 555 points\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Memory Profiler is an open-source module that uses&nbsp;the psutil&nbsp;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&nbsp;@profile&nbsp;decorator can &#8230; <a title=\"How much memory does my Python code use? Memory Profiler!\" class=\"read-more\" href=\"http:\/\/www.mrmarkyoung.com\/oracle\/2022\/03\/16\/how-much-memory-does-my-python-code-use-memory-profiler\/\" aria-label=\"Read more about How much memory does my Python code use? Memory Profiler!\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-419","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/comments?post=419"}],"version-history":[{"count":4,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":423,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/419\/revisions\/423"}],"wp:attachment":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}