In article <11**********************@r35g2000prh.googlegroups .com>,
upperclass <th********@gmail.comwrote:
>Hi,
I'm trying to find a decent way to measure program running time.
I know clock() is probably the standard way of doing it but clock_t
overflows too quickly.
The target program running time ranges from a few seconds to a day.
Is there a decent+portable way to deal with this?
If wall-clock time is a close enough approximation, you can use time_t,
time(), and difftime().
If you need running time but you can arrange to call clock() often enough
to catch it between wraparounds, that may work. (You can at least detect
wraparound and tell how often it's happened; I don't know off the top of
my head whether there's a way to tell how much time a single wraparound
cycle represents. If clock_t is required to be an unsigned type, then
it should be ((clock_t)-1)/CLOCKS_PER_SEC seconds.)
If you need actual running time and catching clock_t wraparounds
doesn't qualify as a "decent" way to deal with it, then there's no
decent and portable way.
(There are fairly easy ways to get the information from outside the
program on most OSs most people are likely to encounter. If you're on
a unix-like system[1], you can use time to get wall-clock and actual
running time reported when it finishes, or you can use ps or top to look
up running time while the program is running; under Windows, you can
get the total CPU time a running process has used from the task manager.
If you're using system tools to get times for running processes, you'll
probably want to add something like
--------
puts("Done; if you want to check the total running time, do it now.");
puts("Press Enter to continue");
getchar();
--------
to the bottom of main() just before it returns.)
dave
[1] I don't have a Mac to try it on, but I strongly suspect that MacOS is
sufficiently unix-like for this to work. Any BSD, Linux, or UNIX(TM)
derivative certainly will be.
--
Dave Vandervies
dj******@csclub.uwaterloo.ca
I don't know what happens when you buy a car, but it must
be DEBILITATING. CRIPPLING.
--Maarten Wiltink in the scary devil monastery