473,378 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How can I time how much each thread takes?

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.

Apr 4 '07 #1
2 1295
In <11*********************@o5g2000hsb.googlegroups.c om>,
si***************@gmail.com wrote:
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
Apr 4 '07 #2
On Apr 3, 11:00 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
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

Apr 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.