title: Finding Memory Usage, CPU utilization, Execution time for running a python script
body: <p>I am using python, and suppose i had some code as below</p>
  
  <p><strong>example.py</strong></p>
  
  <pre><code>import os, psutil
  import MySQLdb as mdb
  
  conn = mdb.connect(user='root', passwd='redhat', db='File_Data', host='localhost', charset="utf8")
  file_path = "/home/local/user/Module/File_processing/part-file.txt"
  p = psutil.Process(os.getpid())
  cursor = conn.cursor()
  
  for line in file_open:
      result = line.split('\t')
      query = "insert into PerformaceReport (campaignID, keywordID, keyword, avgPosition)"
      query += " VALUES (%s,%s,'%s',%s)%(result[0],result[1],result[2],result[3])"
      cursor.execute( query )
  conn.commit()
  
  print p.get_cpu_times()
  print p.get_memory_percent()
  </code></pre>
  
  <p>The above code reads data from text file and saves to database and its working fine</p>
  
  <p>I am running the file with the command <code>python path/to/the/example.py</code></p>
  
  <p>Now what i am trying to do is finding/capturing the below three values/information</p>
  
  <ol>
  <li>The total <code>execution time</code> taken by the python script(<code>example.py</code>) to read and saves data to the database</li>
  <li><code>CPU utilization</code> when the python script(<code>example.py</code>) has run(executed)</li>
  <li><code>Memory usage</code> when the python script(<code>example.py</code>) has run(executed)</li>
  </ol>
  
  <p>I already googled a lot and found some information  <a href="https://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python">here</a>, <a href="https://stackoverflow.com/questions/938733/total-memory-used-by-python-process">here</a> and <a href="https://stackoverflow.com/questions/110259/python-memory-profiler">here</a>, each explanation has different approach and i am really confused and unable to understand the approach on how to capture the above three values when the script <code>example.py</code> has run so asked in SO</p>
  
  <p>Can anyone please provide me an working example(code) that suits/captures the above values for my need i mentioned above, so that i can extend further to my further needs</p>
  
  <p><strong>Edited</strong> </p>
  
  <p>Process Followed</p>
  
  <p><strong>1 . For finding execution time</strong></p>
  
  <p>Run the example.py file with the command as below</p>
  
  <pre><code>  python -m cProfile -o file.profile example.py 
  </code></pre>
  
  <p><strong>2 . For finding CPU Usage</strong></p>
  
  <p>Executing the <code>top</code> command in a terminal after the example.py has runned</p>
  
  <p><strong>Result:</strong>  </p>
  
  <pre><code>   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1354 root      20   0  138m  17m 7200 S  2.0  0.6   2:40.50 Xorg                                             
    2672 shivakri  20   0  581m  19m  12m S  1.3  0.6   0:07.60 gnome-terminal                                   
    2587 shivakri  20   0  987m 168m  33m S  1.0  5.6   5:25.26 firefox                                          
    2780 shivakri  20   0 1233m  51m  16m S  1.0  1.7   0:16.51 gedit                                            
    2163 shivakri  20   0  821m 135m  43m S  0.7  4.5   4:58.74 chrome                                           
    1938 shivakri  20   0  567m  15m  11m S  0.3  0.5   0:06.64 metacity  
    ........
    ........
  </code></pre>
  
  <p>Now in the above result, how/where can i find the CPU/Memory utilization values, whether i need to take the values <code>%CPU %MEM</code> from the line that contains <code>gnome-terminal</code> ? (Because we are running the file through terminal ?)</p>
  
  <p>Aslo <code>top</code> is used for finding total cpu utilization processes that are running, but we need to find the cpu utilization for only example.py script when it is executed/running, i mean how much CPU, Memory is utilized when we run the file(example.py) </p>
  
  <p>Finally what my intention is when i run the python script (example.py) the values such as execution time,CPU utilization taken for running the script,and Memory used for running the script has to be recorded/stored some where(In to a text file) or something</p>