How can I time how much each thread takes?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Apr 4 02:41:01 EDT 2007


In <1175662835.758899.13340 at o5g2000hsb.googlegroups.com>,
silverburgh.meryl at 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



More information about the Python-list mailing list