1

I have a class that contains a methodT to be used by another class methodM, and I want to time the run time of methodT.

Here are the things I've tried

Class MyClass(threading.Thread):
    def __init__():
       threading.Thread.__init__(self)

    def run_porcess(self, args)
       # start a subprocess and print output

    def run(self):
       # ==========================================
       # method A:
       # ==========================================
       start_time = time.clock()
       self.run_process('args')
       elapsed = time.clock() - start_time
       print(elapsed)

       #===========================================
       # method B:
       #===========================================
       timeit.timeit('self.run_process("args")')     

With method A, I can get an result, but I believe it is a less accurate one compared to one that timeit gives.

However, with method B, timeit will produce an NameError as it doesn't recognize self.run_process

I've read the this post, and tried adding the timewrapper method

def timewrapper(args)                       
     return self.run_process(args)

it just doesn't work for me as it gives a NameError: global name 'timewrapper' is not defined.

My question is how do I correctly implement the method B using timeit?

NOTE: I cannot use modules other than core python

Community
  • 1
  • 1
user1948847
  • 955
  • 1
  • 12
  • 27

1 Answers1

-1

What version of python are you using? In version 3.3 time.clock() is deprecated and it is recommended you use perf_counter() or process_time(). That could contribute to the inaccuracy.

GleasonK
  • 1,032
  • 8
  • 17