943,723 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2655
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 3rd, 2009
0

How to get a thread's execution time

Expand Post »
Hi everyone

I've been looking into boost and c++ reference, as well as some googling, and I cannot find a way to get a thread's execution time, all options I found are related to system time and I need only the execution time for the thread (inside the thread's context). Anyone knows how to get this?

Thanks in advance
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Jul 3rd, 2009
0

Re: How to get a thread's execution time

depends on the thread, but many times execution time is not measurable because it happens too quickly.

But generally you would call time functions when the thread starts and again when it ends, then execution time is the difference between those two times. Call clock() to get time in milliseconds (for most operating systems). On MS-Windows you can use QueryPerformanceCounter()
Last edited by Ancient Dragon; Jul 3rd, 2009 at 1:37 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 3rd, 2009
0

Re: How to get a thread's execution time

You are in a multi-threaded environment. Other threads in your application are taking time slices. Other applications are pre-empting and taking time slices. What I usually do is bump my application priority up to real time, set that's thread priority to real time., and then call the function N=10,000 times or so and divide by N to get an approximate time.

The RDTSC assembly instruction can be called before and after and the difference taken!

  1. static uint tclkl, tclkh;
  2.  
  3. void CpuDelaySet(void)
  4. {
  5. __asm {
  6. rdtsc ; Read time-stamp counter
  7.  
  8. mov tclkl,eax ; Save low 32bits
  9. mov tclkh,edx ; Save high 32bits
  10. };
  11. }
  12.  
  13.  
  14. uint CpuDelayCalc(void)
  15. {
  16. uint v;
  17.  
  18. __asm {
  19. rdtsc ; Read time-stamp counter
  20.  
  21. sub eax,tclkl
  22. sbb edx,tclkh ; edx:eax = total elapsed interval
  23. mov v,eax
  24. };
  25.  
  26. return v;
  27. }
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 4th, 2009
0

Re: How to get a thread's execution time

Like wildgoose pointed out, in a multi-threaded environment if one uses the system functions like clock or time will not be able to measure the thread's execution time but only the global (process) time.

Wildgoose, I can't understand your method. Is it possible to explain a bit more?

Tks
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Jul 4th, 2009
0

Re: How to get a thread's execution time

In its simplest form....

  1.  
  2. uint nRepeat = 10000;
  3. uint nTotTime;
  4. double fTime;
  5.  
  6. CpuDelaySet();
  7.  
  8. for (uint n = 0; n < nRepeat; n++)
  9. {
  10. vD = MyFunc( vA, vB );
  11. }
  12. nTotTime = CpuDelayCalc();
  13.  
  14. nOnce = nTotTime / nRepeat;
  15. or
  16. nOnce = (nTotTime + (nRepeat>>1)) / nRepeat;
  17. or
  18. fTime = ((double)nTotTime) / ((double)nRepeat);


Note that I'm only returning a 32-bit value so the idea is to not overflow it! 0xffffffff ( 4,294,967,295)

The idea is to repeat the same test N times. In this example I chose 10,000 times. But I recommend to start around 1000 and work up until total time doesn't exceed a 32-bit unsigned value!

This is essentially an average result. You are still being pre-empted, then numbers will be all over the place each run. But they'll be mostly in the ball park. I use this technique to see if optimizations to my function make the function's time increase or decrease!

The RDTSC instruction is a 64-bit value, which contains the number of clock cycles that have elapsed and is accessible from the Application Ring meaning non-system software have access to it! Win32 hasn't blocked access so it is available for reading. It is set to zero at processor reset and merely rolls over to zero when the high count is reached!
Last edited by wildgoose; Jul 4th, 2009 at 1:39 pm. Reason: code twiddle
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 6th, 2009
0

Re: How to get a thread's execution time

Hi,

Ok, I don't need to totally understand how you do it to use it, but my purposes are different from benchmarking. This averaging is a very clever idea indeed, but I will not run the same function for N times. I have N threads running the same function and every thread needs to know for how long it has executed already. I'm thinking that this method is not applicable to such case...
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Jul 6th, 2009
0

Re: How to get a thread's execution time

AFAIK it is not possible to time a specific thread as if that thread were the only thing running on the operating system (windows in this case). One reason for that is because the os will perform thousands of context switches while the thread is running, so any time you try to calculate will include the time all those other things are doing as well. So any profiling you attempt will only be approximates, not absolutes.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 6th, 2009
0

Re: How to get a thread's execution time

Anything you try will be ballpark. One work around is to run test like I said, then encode into your program the approximate average that gets added to a bucket for each worker thread doing the same task. It won't be accurate, but will be in the ballpark. I think its the best you're going to do.

But keep in mind that it won't be accurate. Don't forget to get the samplings in a release build with your optimization turned on but make sure it is outside the scope or the optimizer will re-arrange your code and the tracking tags won't be where you think they are!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 6th, 2009
0

Re: How to get a thread's execution time

If you're trying to monitor worker thread usage, then keeping a task count per thread would be just as effective!

You mentioned several threads doing the same job thus that indicates worker threads. I'm assuming you have a number crunching task so find the number of CPU's you have then multiply by two. That is the number of worker threads you'll need for that one task to be most efficient and to run your processor dry. You can request which processor a thread is spawned from but the processor decides. Though you can override it. Over request your threads the read which CPU it is running on. Once you have the distribution you want, then release the ones you don't want! Kind of crude but its the only way I know to override the Operating System logic. Because as I mentioned, you're only requesting a CPU, that doesn't mean it has to give it to you!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 6th, 2009
0

Re: How to get a thread's execution time

A task counter would not solve the problem because what I want is the workers to stop after x elapsed time.

In the meanwhile I found this thread:
http://www.linuxforums.org/forum/lin...cess-time.html

which obviously is for linux only. I don't want to have to read the /proc... file every time I need the execution time so I am trying to use clock_gettime(...) method. There is still the issue system/user time. Assuming this will not make a difference for me I tried then the posix method, but sometimes the second value read is bigger than the first (diff is negative), which does not make much sense. I tried to find the reference for this function in order to know more details on why this happens but I didn't find it. Any idea (or link)?

From what I could understand there is no such (or similar) thing for windows, so I am still limited, since I'm building a supposedly cross-platform library...
Last edited by rmlopes; Jul 6th, 2009 at 2:17 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Drawing large bitmap/background
Next Thread in C++ Forum Timeline: Buying machine





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC