views:

203

answers:

3

Hello, I'm trying to count the execution time of part of my application, but since I need to get milliseconds, and I need to get long execution times too. I'm currently using clock_t = clock() from ctime, but it has a range of only 72 minutes I think, which is not suitable for my needs. Is there any other portable way to count large execution times keeping millisecond precision? Or some way so overcome this limitation of clock_t ?

+2  A: 

Unfortunately there are none that I know of that are cross-platform (that's not to say there doesn't exist any, however).

Nevertheless, it is easy enough to work around this problem. Just create a separate thread (ex: boost.thread) which sleeps for a long time, adds the time difference so far to a total, then repeats. When the program is shut down, stop the thread where it can also add to this counter before it quits.

A: 

Set up separate counters for milliseconds, seconds, minutes, etc. When milliseconds get to 999, add 1 to seconds, when they get to 59 add 1 to minutes. Or you could just set up 2 timers, one for durations up to 60 minutes (ie 60*60*1000 milliseconds) and one for hours.

High Performance Mark
So is there any portable way to reliably update a counter after every millisecond of process time?
Mike Seymour
Who said anything about updating it every millisecond? The OP implies that he only needs the total to be correct at fixed points.
dash-tom-bang
+3  A: 

The first question you need to ask is do you really need millisecond precision in time spans over a hour.

If you do one simple method (without looking around for libraries that do it already)is just track when the timer rolls over and add that to another variable.

Scott Chamberlain
The important takeaway is that the OP should accumulate time deltas in a way that will be valid for the expected runtime of the application. There's no reason why you can't accumulate the deltas in a 64 bit integer; 64 bits of milliseconds should be enough for anyone. (It's allows over 584 million years.)
dash-tom-bang