Handling overflow in processor timing using GNU clock() - c

I need to incrementally time some C code on a 32-bit Linux system. I am using the GNU clock() function for this. Here is the skeleton of my code with the relevant clock bits:
clock_t start, end;
double elapsed;
/* Init things */
start = clock();
while(terminationNotMet) {
/* Do some work. */
end = clock();
elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
fprintf(fp,"%lf %d", elapsed, someResults);
}
Now the problem is that the clock_t is really just a long int and this code runs for quite awhile. The elapsed number of clock ticks returned by clock() eventually overflows and the resulting data is useless. Any thoughts on some workarounds or another method of timing CPU time? (Using wall clock time is not an option as these jobs are running niced on multiuser systems)

Unfortunately this is a bug in glibc: clock_t is signed long rather than unsigned long, so it's impossible to use due to overflow. It should work to cast the values to unsigned long before subtracting them, but this is an ugly hack.
A better solution would be to use the modern clock_gettime function with the CLOCK_CPUTIME clock. This will give you nanosecond-resolution results instead of the poor resolution clock gives.

How about updating the start time on each iteration:
elapsed = 0.0;
start = clock();
while(terminationNotMet) {
/* Do some work. */
end = clock();
elapsed += ((double) (end - start)) / CLOCKS_PER_SEC;
fprintf(fp,"%lf %d", elapsed, someResults);
start = end;
}

Related

gettimeofday to calculate elapsed when day changes

The typical example that I see when trying to do measure elapsed time goes something like this:
#include <sys/time.h>
#include <stdio.h>
int main() {
struct timeval start, end;
gettimeofday(&start, NULL);
//Do some operation
gettimeofday(&end, NULL);
unsigned long long end_time = (end.tv_sec * 1000000 + end.tv_usec);
unsigned long long start_time = (start.tv_sec * 1000000 + start.tv_usec);
printf("Time taken : %ld micro seconds\n", end_time - start_time);
return 0;
}
This is great when it's somewhere mid day, but if someone were to run some tests late at night this wouldn't work. My approach is something like this to address it:
#include <sys/time.h>
#include <stdio.h>
int main() {
struct timeval start, end;
gettimeofday(&start, NULL);
//Do some operation
gettimeofday(&end, NULL);
unsigned long long end_time = (end.tv_sec * 1000000 + end.tv_usec);
unsigned long long start_time = (start.tv_sec * 1000000 + start.tv_usec);
unsigned long long elapsed_time = 0;
if ( end_time < start_time )
//Made up some constant that defines 86,400,000,000 microseconds in a day
elapsed_time = end_time + (NUM_OF_USEC_IN_A_DAY - start_time);
else
elapsed_time = end_time - start_time;
printf("Time taken : %ld micro seconds\n", elapsed_time);
return 0;
}
Is there a better way of anticipating day change using gettimeofday?
Despite the name, gettimeofday results do not roll over daily. The gettimeofday seconds count, just like the time seconds count, started at zero on the first day of 1970 (specifically 1970-01-01 00:00:00 +00:00) and has been incrementing steadily ever since. On a system with 32-bit time_t, it will roll over sometime in 2038; people are working right now to phase out the use of 32-bit time_t for this exact reason and we expect to be done well before 2038.
gettimeofday results are also independent of time zone and not affected by daylight savings shifts. They can go backward when the computer's clock is reset. If you don't want to worry about that, you can use clock_gettime(CLOCK_MONOTONIC) jnstead.
Why would you want to handle the case where end.tv_sec is less than start.tv_sec? Are you trying to account for ntpd changes? If so, and especially if you want to record only elapsed time, then use clock_gettime instead of gettimeofday as the former is immune to wall clock changes.
but if someone were to run some tests late at night this wouldn't work
That's an incorrect statement because gettimeofday is not relative to the start of each day but rather relative to a fixed point in time. From the gettimeofday manual:
gives the number of seconds and microseconds since the Epoch
So the first example will work as long as there are no jumps in time (e.g. due to manual time setting or NTP). Again from the manual:
The time returned by gettimeofday() is affected by discontinuous
jumps in the system time (e.g., if the system administrator manually
changes the system time). If you need a monotonically increasing
clock, see clock_gettime(2).
Is there a better way of anticipating day change using gettimeofday?
In addition to other problems identified in various answers, the following is also subject integer overflow when time_t is only 32-bit. Instead scale by a (unsigned) long long constant.
// unsigned long long end_time = (end.tv_sec * 1000000 + end.tv_usec);
long long end_time = (end.tv_sec * 1000000ll + end.tv_usec);
// Use correct specifier, not %ld for unsigned long long
// printf("Time taken : %ld micro seconds\n", elapsed_time);
long long elapsed_time = end_time - start_time;
printf("Time taken : %lld micro seconds\n", elapsed_time);
Tip: enables all compiler warnings.

How to measure execution time of several functions in a loop in C

I hope to find a way to measure execution time of several functions in a for loop in C. For example, there is code like:
for(;;)
{
func1();
func2();
func3();
}
I want to know how much time program spends totally on func1() (or func2, func3).
I know I can use clock() to measure time. However, in this case if I write the code like:
for(;;)
{
a = clock();
func1();
b = clock();
time_func1 += (b-a);
a = clock();
func2();
b = clock();
time_func2 += (b-a);
a = clock();
func3();
b = clock();
time_func3 += (b-a);
}
It looks like too stupid and the result is not accurate.
If you are on Linux, I'd suggest you to use clock_gettime. This enables precise time measurement with several clocks, also with high resolution. clock itself has a low resolution.
Maybe a word regarding its use:
timespec t1, t2;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
/* CODE TO BE MEASURED */
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
clock() has a very low resolution. Depending your OS, there are more accurate functions.
In Windows there's QueryPerformanceCounters.
clock() will also give not the real execution time if multi-threaded code is present.
Sebastian is right,
According to the man page, the POSIX function clock() returns an approximation of the clock ticks (what do they mean by approximation ?) used by the program. You have to divide by the number of clocks per second to get the execution time.
This is done by:
int main() {
clock_t start;
clock_t end;
double cpu_time_used;
start = clock();
# do some computations
end = clock();
cpu_time_used = ((double) (end-start)) / CLOCKS_PER_SEC;
}
It is easy to use and you get the CPU ticks for the calling process, but the precision is in milliseconds.
If you need more precision, the POSIX function clock_gettime() precision is order of nanoseconds. You can specify to retrieve the system-wide, per-process or thread-specific elapsed time. Here's an example:
int main() {
timespec start;
timespec end;
double cpu_time_used;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
# do some computations
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
cpu_time_used = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0;
}
If you want measure the time and further want to optimize those function better to go with profiling tools like oprofile there you will get to know which function is taking how much time.with 'opannotate' you will get to know in better way

clock() - execution time for c function

I am trying to measure the execution time of a code block in C. I have something like this in my code:
clock_t begin, end;
double time_spent;
begin = clock();
ATL_dsymv(122,n,alfa,A,n,X,1,beta,Y,1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf ("(%f seconds)",time_spent);
But it always returns: (0.000000 seconds). I tried the same thing on simpler code blocks like for's but it has the same result. What am I doing wrong? Thanks a lot.
clock usually has very poor resolution, on the order of 10 milliseconds. This is most likely your problem. If you're on a POSIX system, use clock_gettime with the CLOCK_PROCESS_CPUTIME_ID clock to get a high-resolution result. Other types of systems probably have system-specific ways to achieve the same.

Computing algorithm running time in C

I am using the time.h lib in c to find the time taken to run an algorithm. The code structure is somewhat as follows :-
#include <time.h>
int main()
{
time_t start,end,diff;
start = clock();
//ALGORITHM COMPUTATIONS
end = clock();
diff = end - start;
printf("%d",diff);
return 0;
}
The values for start and end are always zero. Is it that the clock() function does't work? Please help.
Thanks in advance.
Not that it doesn't work. In fact, it does. But it is not the right way to measure time as the clock () function returns an approximation of processor time used by the program. I am not sure about other platforms, but on Linux you should use clock_gettime () with CLOCK_MONOTONIC flag - that will give you the real wall time elapsed. Also, you can read TSC, but be aware that it won't work if you have a multi-processor system and your process is not pinned to a particular core. If you want to analyze and optimize your algorithm, I'd recommend you use some performance measurement tools. I've been using Intel's vTune for a while and am quite happy. It will show you not only what part uses the most cycles, but highlight memory problems, possible parallelism issues etc. You may be very surprised with the results. For example, most of the CPU cycles might be spent waiting for memory bus. Hope it helps!
UPDATE: Actually, if you run later versions of Linux, it might provide CLOCK_MONOTONIC_RAW, which is a hardware-based clock that is not a subject to NTP adjustments. Here is a small piece of code you can use:
stopwatch.hpp
stopwatch.cpp
Note that clock() returns the execution time in clock ticks, as opposed to wall clock time. Divide a difference of two clock_t values by CLOCKS_PER_SEC to convert the difference to seconds. The actual value of CLOCKS_PER_SEC is a quality-of-implementation issue. If it is low (say, 50), your process would have to run for 20ms to cause a nonzero return value from clock(). Make sure your code runs long enough to see clock() increasing.
I usually do it this way:
clock_t start = clock();
clock_t end;
//algo
end = clock();
printf("%f", (double)(end - start));
Consider the code below:
#include <stdio.h>
#include <time.h>
int main()
{
clock_t t1, t2;
t1 = t2 = clock();
// loop until t2 gets a different value
while(t1 == t2)
t2 = clock();
// print resolution of clock()
printf("%f ms\n", (double)(t2 - t1) / CLOCKS_PER_SEC * 1000);
return 0;
}
Output:
$ ./a.out
10.000000 ms
Might be that your algorithm runs for a shorter amount of time than that.
Use gettimeofday for higher resolution timer.

I need better solution to get time of execution program in C

I am using this
clock_t start = clock();
QuickSort(0, ItemCount-1,1);
printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
I would like to get time in nanoseconds.
If you're on a Unix-like system you can most likely use the clock_gettime() function. In Linux, it is found in -lrt, most other systems I've tried have it in the system C library.
It should be noted that x86 has an rdtsc instruction that is extremely precise.
Use the struct timespec as follows to obtain the time of execution of the code in nanoseconds precision
struct timespec start, end;
clock_gettime(CLOCK_REALTIME,&start);
/* Do something */
clock_gettime(CLOCK_REALTIME,&end);
It returns a value as ((((unsigned64)start.tv_sec) * ((unsigned64)(1000000000L))) + ((unsigned64)(start.tv_nsec))))
Hope this answer will be more helpful for you to get your desired execution time in nanoseconds.

Resources