Running time of a code

R

Rajeev

I want to calculate the running time of my program, which runs for a
long time(~ a day). This is what I am doing in my code:


#include <time.h>
clock_t tend, tstart;
tstart = clock();

\* Body of Code *\

tend = clock();
Running Time = ((double) (tend - tstart) / (double) CLOCKS_PER_SEC));

But for my codes which run for a long time, I am not getting the right
value for the running time variable. Please help me out!!

Thanks
Rajeev.
 
G

Gordon Burditt

I want to calculate the running time of my program, which runs for a
long time(~ a day).

clock() measures CPU time, which is a lot like football or basketball
game clock time: it doesn't run all the time.
#include <time.h>
clock_t tend, tstart;
tstart = clock();

\* Body of Code *\ ^^ this is a syntax error

tend = clock();
Running Time = ((double) (tend - tstart) / (double) CLOCKS_PER_SEC));
^ extra )

Check out the data type of clock_t and the value of CLOCKS_PER_SEC.
If, for example, clock_t is 32 bits signed and CLOCKS_PER_SEC is
1,000,000 then clock_t overflows at about 0.6 hours.
But for my codes which run for a long time, I am not getting the right
value for the running time variable. Please help me out!!

How do you know it's wrong? If it's negative, it probably overflowed.
If it's just too low, well, a program sitting at an input prompt
for a few decades might well accumulate only a few milliseconds of
CPU time, if that much. Or it might have overflowed.

The only solution I know of to overflow is to take samples more often
(yes, this can be a pain), convert to better units in a floating-point
variable, and add up the results.

Gordon L. Burditt
 
C

Christian Bau

Rajeev said:
I want to calculate the running time of my program, which runs for a
long time(~ a day). This is what I am doing in my code:


#include <time.h>
clock_t tend, tstart;
tstart = clock();

\* Body of Code *\

tend = clock();
Running Time = ((double) (tend - tstart) / (double) CLOCKS_PER_SEC));

But for my codes which run for a long time, I am not getting the right
value for the running time variable. Please help me out!!

Check the values of CLOCKS_PER_SEC and the type of clock_t in your
implementation. Is your implementation capable of handling times that
are a day apart? For example, if clock_t is a 32 bit integer, and
CLOCKS_PER_SEC = 1000000, then this will not work.
 
A

akarl

Rajeev said:
I want to calculate the running time of my program, which runs for a
long time(~ a day). This is what I am doing in my code:


#include <time.h>
clock_t tend, tstart;
tstart = clock();

\* Body of Code *\

tend = clock();
Running Time = ((double) (tend - tstart) / (double) CLOCKS_PER_SEC));

But for my codes which run for a long time, I am not getting the right
value for the running time variable. Please help me out!!

You can probably use a shell command instead (e.g. `time' under GNU/Linux).

August
 
C

Cong Wang

akarl said:
You can probably use a shell command instead (e.g. `time' under GNU/Linux).

August
Gprof on Linux is also better.I think probably overflow occurred.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top