clock() - execution time for c function - c

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.

Related

C (time.h) clock_t = clock() producing wrong duration

Some example code:
clock_t clock_start = clock();
for( ... ) { ... do stuff ... }
clock_t clock_stop = clock();
double duration = 1000.0 * (clock_stop - clock_start) / CLOCKS_PER_SEC;
printf("time: %f ms\n", duration);
When I ran this code it produced an output of:
time: 4756.869000 ms
This is clearly wrong. I estimate the actual time taken is about 10 seconds, and verified this via a stop watch.
There appears to be a factor of about 2 - 3 missing.
Is it possible that CLOCKS_PER_SEC is defined as something nonsensical on my system? (I am using a Raspberry Pi 3, with Raspberry Pi OS.) Is there any way to check this? Or is it more likely that something else is the cause of the issue.
I am aware of alternative methods of measuring time on posix systems. I will implement some tests with one of those as a possible alternative, regardless.
The clock() function returns an approximation of processor time used by the program.
It says "processor time", not the amount of time that a stopwatch would say. If you want to measure the amount of time passing in the real world, you simply need to use one of those other functions.

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

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.

Handling overflow in processor timing using GNU clock()

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;
}

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