How can I time how much each thread takes?

  • Thread starter silverburgh.meryl
  • Start date
S

silverburgh.meryl

Hi,

I have the following code which spawn a number of thread and do
something (in the run method of MyThread).
how can I record how much time does EACH thread takes to complete the
'run'?

for j in range(threadCount):
t = MyThread(testNo)
threads.append(t)
t.start()
testNo += 1


for t in threads:
print "len = %d", len(threads)
t.join()


I have read example of timeit.Timer() funcion, but I don' t know how
can i use it in a multiple thread program?
Thank you for any help.
 
M

Marc 'BlackJack' Rintsch

In <[email protected]>,
I have the following code which spawn a number of thread and do
something (in the run method of MyThread).
how can I record how much time does EACH thread takes to complete the
'run'?

for j in range(threadCount):
t = MyThread(testNo)
threads.append(t)
t.start()
testNo += 1


for t in threads:
print "len = %d", len(threads)
t.join()


I have read example of timeit.Timer() funcion, but I don' t know how
can i use it in a multiple thread program?

If you want wall time then use `time.time()` in the `run()` method of the
threads to calculate the difference between start and end and set an
attribute with the elapsed time on the `MyThread` objects.

Ciao,
Marc 'BlackJack' Rintsch
 
7

7stud

Hi,

I have the following code which spawn a number of thread and do
something (in the run method of MyThread).
how can I record how much time does EACH thread takes to complete the
'run'?

for j in range(threadCount):
t = MyThread(testNo)
threads.append(t)
t.start()
testNo += 1

for t in threads:
print "len = %d", len(threads)
t.join()

I have read example of timeit.Timer() funcion, but I don' t know how
can i use it in a multiple thread program?
Thank you for any help.

How about this:

import time, threading

class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.begin = 0
self.end = 0

def run(self):
print "thread starting in background"
self.begin = time.time()
time.sleep(3)
self.end = time.time()
print "thread ending"

mt = MyThread()
mt.start()
print "main still running in foreground"

print "main going to wait for thread to end ....."
mt.join()

diff = mt.end - mt.begin
print "Thread's run() time was: %.5f" % diff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top