Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: using clock() to measure time in multi-threaded programs

Tags:

c

I've always used clock() to measure how much time my application took from start to finish, as;

int main(int argc, char *argv[]) {   const clock_t START = clock();    // ...    const double T_ELAPSED = (double)(clock() - START) / CLOCKS_PER_SEC; } 

Since I've started using POSIX threads this seem to fail. It looks like clock() increases N times faster with N threads. As I don't know how many threads are going to be running simultaneously, this approach fails. So how can I measure how much time has passed ?

like image 741
Suugaku Avatar asked Jun 03 '10 01:06

Suugaku


People also ask

Does multithreading save time?

Few more advantages of Multithreading are:Multithreading saves time as you can perform multiple operations together. The threads are independent, so it does not block the user to perform multiple operations at the same time and also, if an exception occurs in a single thread, it does not affect other threads.

Does C allow multi threading?

C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature. This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C program using POSIX.

Can multiple threads run concurrently?

Within a process or program, we can run multiple threads concurrently to improve the performance. Threads, unlike heavyweight process, are lightweight and run inside a single process – they share the same address space, the resources allocated and the environment of that process.

What is multi threaded programming used for?

Multithreading is the ability of a program or an operating system to enable more than one user at a time without requiring multiple copies of the program running on the computer. Multithreading can also handle multiple requests from the same user.


2 Answers

clock() measure the CPU time used by your process, not the wall-clock time. When you have multiple threads running simultaneously, you can obviously burn through CPU time much faster.

If you want to know the wall-clock execution time, you need to use an appropriate function. The only one in ANSI C is time(), which typically only has 1 second resolution.

However, as you've said you're using POSIX, that means you can use clock_gettime(), defined in time.h. The CLOCK_MONOTONIC clock in particular is the best to use for this:

struct timespec start, finish; double elapsed;  clock_gettime(CLOCK_MONOTONIC, &start);  /* ... */  clock_gettime(CLOCK_MONOTONIC, &finish);  elapsed = (finish.tv_sec - start.tv_sec); elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 

(Note that I have done the calculation of elapsed carefully to ensure that precision is not lost when timing very short intervals).

If your OS doesn't provide CLOCK_MONOTONIC (which you can check at runtime with sysconf(_SC_MONOTONIC_CLOCK)), then you can use CLOCK_REALTIME as a fallback - but note that the latter has the disadvantage that it will generate incorrect results if the system time is changed while your process is running.

like image 115
caf Avatar answered Oct 03 '22 10:10

caf


What timing resolution do you need? You could use time() from time.h for second resolution. If you need higher resolution, then you could use something more system specific. See Timer function to provide time in nano seconds using C++

like image 45
Manfre Avatar answered Oct 03 '22 10:10

Manfre