Calculate the elapsed time

Windows SDK uses DWORD GetTickCount() functions to calculate a elapsed time between 2 points.


DWORD start = GetTickCount();
//Do some work
Sleep(10);
DWORD end = GetTickCount();
DWORD elapsed = end - start;


Linux way of calculating

Using gettimeofday function is the most general method.


#include <stdio.h> // for printf()
#include <sys/time.h> // for clock_gettime()
#include <unistd.h> // for usleep()

int main() {
    struct timeval start, end;
    long secs_used,micros_used;

    gettimeofday(&start, NULL);
    usleep(1250000); // Do the stuff you want to time here
    gettimeofday(&end, NULL);

    printf("start: %ld secs, %ld usecs\n",start.tv_sec,start.tv_usec);
    printf("end: %ld secs, %ld usecs\n",end.tv_sec,end.tv_usec);

    secs_used=(end.tv_sec - start.tv_sec); //avoid overflow by subtracting first
    micros_used= ((secs_used*1000000) + end.tv_usec) - (start.tv_usec);
    float secs_elapsed = micros_used * 1.0 / 1000000;
    printf("micros_used: %ld\n",micros_used);
    printf("sec used: %10.6f\n",secs_elapsed);
    return 0;
}


Let's compile and run.


root@spypiggy-ubuntu:/usr/local/src/study/cpp/clock# ./a.out
start: 1571656780 secs, 330873 usecs
end: 1571656781 secs, 580973 usecs
micros_used: 1250100
sec used:   1.250100



There are other functions like clock, clock_get_time. But clock function does not calculate the elapsed time. It calculates the tick count of processor consumming time.


#include <stdio.h> // for printf()
#include <sys/time.h> // for clock_gettime()
#include <time.h> // for clock()
#include <unistd.h> // for usleep()
#include <math.h>       /* sqrt */

int frequency_of_primes (int n) {
  int i,j;
  int freq=n-1;
  for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}
  return freq;
}

int main() {
    struct timeval start, end;
    clock_t start_clock, end_clock;
    long secs_used,micros_used;

    // CPU Intensive work
    gettimeofday(&start, NULL);
    start_clock = clock();
    usleep(1000000); // Do the stuff you want to time here
    frequency_of_primes (250000);
    end_clock = clock();
    gettimeofday(&end, NULL);


    printf("CPU intensive clock start: %lf secs\n", (float)start_clock / CLOCKS_PER_SEC );
    printf("CPU intensive clock end  : %lf secs\n", (float)end_clock   / CLOCKS_PER_SEC );
    printf("CPU intensive clock sec used: %10.6f\n", (float)(end_clock - start_clock)/ CLOCKS_PER_SEC);


    secs_used=(end.tv_sec - start.tv_sec); //avoid overflow by subtracting first
    micros_used= ((secs_used*1000000) + end.tv_usec) - (start.tv_usec);
    float secs_elapsed = micros_used * 1.0 / 1000000;
    printf("sec used: %10.6f\n",secs_elapsed);

    //No CPU Work
    gettimeofday(&start, NULL);
    start_clock = clock();
    usleep(1000000); // Do the stuff you want to time here
    //frequency_of_primes (250000);
    end_clock = clock();
    gettimeofday(&end, NULL);


    printf("CPU sleep clock start: %lf secs\n", (float)start_clock / CLOCKS_PER_SEC );
    printf("CPU sleep clock end  : %lf secs\n", (float)end_clock   / CLOCKS_PER_SEC );
    printf("CPU sleep clock sec used: %10.6f\n", (float)(end_clock - start_clock)/ CLOCKS_PER_SEC);


    secs_used=(end.tv_sec - start.tv_sec); //avoid overflow by subtracting first
    micros_used= ((secs_used*1000000) + end.tv_usec) - (start.tv_usec);
    secs_elapsed = micros_used * 1.0 / 1000000;
    printf("sec used: %10.6f\n",secs_elapsed);
    return 0;
}



While the sleep function does not use the CPU, frequency_of_primes function needs much CPU processing time.

First the program sleeps 1 seconds and call frequency_of_primes function to consume CPU.
Then the program just sleeps 1 seconds.
Compile and run it.


root@spypiggy-ubuntu:/usr/local/src/study/cpp/clock# g++ clock2.cpp
root@spypiggy-ubuntu:/usr/local/src/study/cpp/clock# ./a.out
CPU intensive clock start: 0.000637 secs
CPU intensive clock end  : 0.239504 secs
CPU intensive clock sec used:   0.238867
sec used:   1.239443
CPU sleep clock start: 0.239575 secs
CPU sleep clock end  : 0.239584 secs
CPU sleep clock sec used:   0.000009
sec used:   1.000459


As you can see , only the first part uses clock time, second part almost don't use clock time.
So you don't have to use clock function for calculating the elapsed time.
The clock function just calculates the CPU processing time.



댓글

이 블로그의 인기 게시물

MQTT - C/C++ Client

RabbitMQ - C++ Client #1 : Installing C/C++ Libraries

C/C++ - Everything about time, date