0

I am a complete beginner using python and I been asked to log the profile for every time I run a given a simulation. My simulation has 4 classes and 10 methods that run in a loop until aa certain condition is achieved.

I have written the follow file that creates the file:

LOG_FILE = open(os.path.join(os.getcwd(), "logs", "Log_Log_{}.txt".format(
   str(datetime.datetime.now()).replace(":", "_"))), mode="w") 

Right at the bottom of the script I added:

if __name__ == "__main__":
    cProfile.run("main()", LOG_FILE, sort="tottime")

Why is my Log_Log file blank and cProfile not returning anything?

Ian_De_Oliveira
  • 291
  • 5
  • 16

1 Answers1

-1

use pstats to dump the output of cProfile in a human readable format

def sample():
   # initialize cProfile
   profiler_object = cProfile.Profile()
   profiler_object.enable()

   # execute something here
   a = [i for i in range(0, 100)]

   profiler_object.disable()

   # dump the profiler stats 
   s = io.StringIO()
   ps = pstats.Stats(profiler_object, stream=s).sort_stats('cumulative')
   ps.dump_stats('enter your dump file path here')

   # convert to human readable format
   out_stream = open('enter your log file path here', 'w')
   ps = pstats.Stats('enter your dump file path here', stream=out_stream)
   ps.strip_dirs().sort_stats('cumulative').print_stats()
   return True

sample()

Sample output:

Sat Sep  8 07:07:34 2018    your_dump_file_path

     2 function calls in 0.000 seconds

     Ordered by: cumulative time

     ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000    <ipython-input-31-4e18ee895e20>:4(<listcomp>)
        1    0.000    0.000    0.000    0.000    {method 'disable' of '_lsprof.Profiler' objects}

You could improvise this by using decorators

def profiler(func):
     def wrapper(*args, **kwargs):
         # profiler initialization 
         res = func(*args, **kwargs)
         # further processing
     return wrapper


@profiler
def a():
    print('hello world')
Adarsh
  • 3,273
  • 3
  • 20
  • 44
  • hi what you mean by ## all profiling code , followed by ## remaining code? For example I have 4 classes running with several methods inside. Where do i put the profiler? tks – Ian_De_Oliveira Sep 08 '18 at 02:13
  • you could get all the methods inside the class as stated here: https://stackoverflow.com/questions/37075680/run-all-functions-in-class and run the profiler as shown above (or) you could decorate the class as stated here: https://stackoverflow.com/questions/6307761/how-can-i-decorate-all-functions-of-a-class-without-typing-it-over-and-over-for and run the profiler. By #all profiling code, i mean profiler initialization and by remaining code, i mean conversion to human readable format and other operations such as plotting a graph (if reqd.) etc – Adarsh Sep 08 '18 at 02:23