469,603 Members | 2,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

measuring running time

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?

Thank you.

Apr 26 '07 #1
2 4410
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
Apr 26 '07 #2
upperclass <th********@gmail.comwrites:
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?
You could check the return value of clock() from time to time
while the program runs and keep a running total.
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Apr 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Edg Bamyasi | last post: by
5 posts views Thread by chellappa | last post: by
3 posts views Thread by SSG | last post: by
4 posts views Thread by Rajeev | last post: by
reply views Thread by Rajeev | last post: by
20 posts views Thread by upperclass | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.