satya
satya

Reputation: 479

How to measure wallclock time in C++ instead of cpu time?

I would like to measure wallclock time taken by my algorithm in C++. Many articles point to this code.

clock_t begin_time, end_time;
begin_time = clock();
Algorithm();
end_time = clock();
cout << ((double)(end_time - begin_time)/CLOCKS_PER_SEC) << endl;

But this measures only cpu time taken by my algorithm. Some other article pointed out this code.

double getUnixTime(void)
{
    struct timespec tv;

    if(clock_gettime(CLOCK_REALTIME, &tv) != 0) return 0;

    return (tv.tv_sec + (tv.tv_nsec / 1000000000.0));
}
double begin_time, end_time;
begin_time = getUnixTime();
Algorithm();
end_time = getUnixTime();
cout << (double) (end_time - begin_time) << endl;

I thought it would print wallclock time taken by my algorithm. But surprisingly, the time printed by this code is much lower than cpu time printed by previous code. So, I am confused. Please provide code for printing wallclock time.

Upvotes: 0

Views: 775

Answers (2)

user11499890
user11499890

Reputation: 131

Those times are probably down in the noise. To get a reasonable time measurement, try executing your algorithm many times in a loop:

const int loops = 1000000;
double begin_time, end_time;
begin_time = getUnixTime();

for (int i = 0; i < loops; ++i)
    Algorithm();

end_time = getUnixTime();
cout << (double) (end_time - begin_time) / loops << endl;

Upvotes: 2

Petr Skocik
Petr Skocik

Reputation: 60067

I'm getting approximately the same times in a single threaded program:

#include <time.h>
#include <stdio.h>
__attribute((noinline)) void nop(void){}
void loop(unsigned long Cnt) { for(unsigned long i=0; i<Cnt;i++) nop(); }
int main()
{
    clock_t t0,t1;
    struct timespec ts0,ts1;
    t0=clock();
    clock_gettime(CLOCK_REALTIME,&ts0);
    loop(1000000000);
    t1=clock();
    clock_gettime(CLOCK_REALTIME,&ts1);
    printf("clock-diff: %lu\n", (unsigned long)((t1 - t0)/CLOCKS_PER_SEC));
    printf("clock_gettime-diff: %lu\n", (unsigned long)((ts1.tv_sec - ts0.tv_sec)));
}
//prints 2 and 3 or 2 and 2 on my system

But clocks manpage only describes it as returning an approximation. There's no indication that approximation is comparable to what clock_gettime returns.

Where I get drastically different results is where I throw in multiple threads:

#include <time.h>
#include <stdio.h>
#include <pthread.h>
__attribute((noinline)) void nop(void){}
void loop(unsigned long Cnt) {
    for(unsigned long i=0; i<Cnt;i++) nop();
}
void *busy(void *A){ (void)A; for(;;) nop(); }
int main()
{
    pthread_t ptids[4]; 
    for(size_t i=0; i<sizeof(ptids)/sizeof(ptids[0]); i++)
        pthread_create(&ptids[i], 0, busy, 0);
    clock_t t0,t1;
    struct timespec ts0,ts1;
    t0=clock();
    clock_gettime(CLOCK_REALTIME,&ts0);
    loop(1000000000);
    t1=clock();
    clock_gettime(CLOCK_REALTIME,&ts1);
    printf("clock-diff: %lu\n", (unsigned long)((t1 - t0)/CLOCKS_PER_SEC));
    printf("clock_gettime-diff: %lu\n", (unsigned long)((ts1.tv_sec - ts0.tv_sec)));

}
//prints 18 and 4 on my 4-core linux system

That's because both musl and glibc on Linux use clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) to implement clock() and the CLOCK_PROCESS_CPUTIME_ID nonstandard clock is described in the clock_gettime manpage as returning time for all process threads together.

Upvotes: 0

Related Questions