473,804 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(threadCou nt):
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 1319
In <11************ *********@o5g20 00hsb.googlegro ups.com>,
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(threadCou nt):
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.comwr ote:
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(threadCou nt):
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(thread ing.Thread):
def __init__(self):
threading.Threa d.__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
4614
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 process starts is not especially important, but it must be complete within a small number of seconds. The operations I am performing do not take a long time (hundreds of milliseconds), but as each part of the process is complete, my worker thread...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10569
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.