2

the suggested method of estimating the time is using the clock() function and then dividing the count of cpu cycles by cycles/second.

My problem is that the program i am trying to run takes a lot of time (in hours). This means the clock() function (that returns a long int) returns a garbage value (because max long int is not big enough for this)

Any suggestions (apart from estimating time for internal loops and adding them up or something) ?

long t1 = clock();
function();
long t2=clock();

time = ((double)t2 - t1) / CLOCKS_PER_SEC
AnkurVj
  • 7,958
  • 10
  • 43
  • 55
  • The suggested method? By who? I'd suggest using a profiler for estimating run/process time. – Edward Strange Nov 12 '10 at 21:49
  • if you don't need millisecond accuracy, you could use time() – Nick Van Brunt Nov 12 '10 at 21:50
  • trying to estimate running time of parts of a program .. will it work good there too ? – AnkurVj Nov 12 '10 at 21:51
  • Keep in mind that if you use `time()` it will give you total elapsed time, not necesarrily how many CPU cycles your program used. In a preemptive system (like windows or linux) a thread can run at 100% for 1 second, be pre-empted and sleep for 1 second, and then run at 100% for 1 second, and the time would be resported as 3 seconds. But in reality your program used only 2 seconds of CPU time. – John Dibling Nov 12 '10 at 22:34
  • @Noah, no a profiler will tell you the proportion of time spent in various parts of a program. In order to do that, there is usually some which makes the wall-clock time of running the program longer. So if you want an estimate of how long you will have to wait for the results to come out, rather than a profile of what is taking the time, a profiler is useless. – Pete Kirkham Nov 12 '10 at 23:39
  • "no a profiler will tell you the proportion of time spent in various parts of a program" - That statement is just plain wholly false. – Edward Strange Nov 13 '10 at 00:20
  • @Noah the first column of flat profile in gprof is the percent time in each function http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html#SEC5 the percentage column in sleepy http://sleepy.sourceforge.net/results.png the cpu time per function in the performance profile in VTune http://software.intel.com/en-us/articles/intel-vtune-amplifier-xe/ the % focus in http://www.ibm.com/developerworks/rational/library/content/03July/2000/2315/2315_fig7.jpg . What profiler have you been using which only gives the total time for a program's execution? – Pete Kirkham Nov 13 '10 at 20:54
  • read it wrong. Thought you where saying profilers don't show time spent, which would be quite silly because that's exactly what a profiler does. – Edward Strange Nov 15 '10 at 17:05

7 Answers7

5

If you are running it under Linux / Unix you can just say:

time my_process
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • u mean i run the process with this command and it will give me the time that it ran for ? – AnkurVj Nov 12 '10 at 21:52
  • 3
    Exactly. It will give you a nice summary of the total elapsed time, both in terms of the real (actual) time as well as the user and system level times. – Justin Ethier Nov 12 '10 at 21:58
3

Think of the problem in terms of percentage error.

If your program takes hours to run, (suppose 4 hours... or 14,400 seconds), then 1/2% error is 72 seconds, or slightly more than a minute.

In other words, to get an accurate answer to within 1/2 of one percent, you only need to time to the nearest minute. Assuming a hand-held stopwatch can time to within a second, or even 1/10th of a second, your error will be insignificantly small.

My point is, you could get an accurate measure of your program just using a seconds-timer or a handheld stopwatch.

abelenky
  • 63,815
  • 23
  • 109
  • 159
1

If the program is long running then sub-second accuracy is probably not particularly important. In that case, why not just call time()?

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • yeah i dont want sub-second accuracy ... is time() a good way to estimate running times .i am trying to compare running time of various algorithms on huge graphs. – AnkurVj Nov 12 '10 at 21:55
0
#include <time.h>
int main()
{

clock_t t1,t2;
t1=clock();
//write your code here

t2=clock();
float runningTime ((float)t2-(float)t1);
//runningTime contains the time
return 0;

}

Pawan Kumar
  • 2,132
  • 2
  • 16
  • 10
0

Boost timers work well.

The timer library supplies a timer class for measuring elapsed time, a progress_timer class for reporting elapsed time, and a progress_display class for displaying an indication of progress toward a goal.

j0k
  • 22,600
  • 28
  • 79
  • 90
Dave McClelland
  • 3,385
  • 1
  • 29
  • 44
0

gettimeofday()

#include <sys/time.h>

int gettimeofday(struct timeval *tv, NULL);

The functions gettimeofday() and settimeofday() can get the time. The tv argument is a struct timeval (as specified in ):

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

time_t is at least 32 bit, so it can store hours.

osgx
  • 90,338
  • 53
  • 357
  • 513
0

I would go with the getTimeofTheDay kind of an approach (suggested by @osgx) if the precision (given by win calls like queryPerformanceTimer) is not important.

TJ-
  • 14,085
  • 12
  • 59
  • 90