Duration measurement in C - alternative to clock()

Duration measurement in C - alternative to clock()

It turns that C-clock() returns the >sum< of cpu usages of all
threads. Hence clock() cannot be used to measure speedups.
Here an alternative:


struct timeval tim;
gettimeofday(&tim, NULL);
double t0=tim.tv_sec+(tim.tv_usec/1000000.0);
*** long multi-threaded actions here ***
gettimeofday(&tim, NULL);
double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("Duration: %.3lf\\n", t1-t0);

I suspect that this works only if other processes are not interfering/ busy.
I have been doing runs three times in order to assess the quality of
the findings.

4 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

You might like to look at the OpenMP time function, even though you're using native threads:




double start, end;



start = omp_get_wtime();

... Stuff to time

end = omp_get_wtime();

printf(" Took %f seconds.\n", end-start);

Have not seen omp_get_wtime() on the Web.
What is the semantics?

A process does a remote call to a server in Singapore which returns
after 10 minutes.
Assume clock() returns a zero duration - no cpu usage while waiting.

gettimeofday() measures 600 sec (assume no interference by other processes)

What is omp_get_wtime() measuring?

A process spawns 2 threads and wait for their completion.
Both threads complete after 10 minutes.
clock() measures the equivalence of 1200 sec.
gettimeofday() measures 600 sec
What is omp_get_wtime() measuring?

double omp_get_wtime();

  • Provides a portable wall clock timing routine

Returns a double-precision floating point value equal to the number of elapsed seconds since some point in the past. That point will remain consistent during program execution, making subsequent comparisons possible.


// omp_get_wtime.cpp
// compile with: /openmp
#include "omp.h"

int main() {
double start = omp_get_wtime( );
double end = omp_get_wtime( );
double wtick = omp_get_wtick( );

printf_s("start = %.16g\nend = %.16g\ndiff = %.16g\n",
start, end, end - start);

printf_s("wtick = %.16g\n1/wtick = %.16g\n",
wtick, 1.0 / wtick);

start = 594255.3671159324 end = 594256.3664474116 diff = 0.9993314791936427 wtick = 2.793651148400146e-007 1/wtick = 3579545

Faça login para deixar um comentário.