0

I'm trying to record the time my program takes to finish in seconds, with sub-second accuracy.

I'm not recording CPU time or cycles, I simply want to be able to mark a start point (Wall Clock time), then at the end of my program mark a finish (Wall Clock time), and calculate the delta.

Richard
  • 56,349
  • 34
  • 180
  • 251
Breeduss
  • 29
  • 1
  • 3
  • 1
    What operating system? – clearlight Feb 04 '17 at 23:13
  • 2
    No answers have been accepted yet by you. If you click the √ by one of the answers (the one that solves your problem, if any) you will get reputation points and also benefit/credit the person who helped you solve the problem. See the [2 Minute Tour](http://stackoverflow.com/tour) to get an idea how the Q&A and rep system works here. – clearlight Feb 05 '17 at 01:55

5 Answers5

1

Have a look at the function:

   int clock_gettime(clockid_t clk_id, struct timespec *tp);

The function will fill the structure struct timespec you provide. Here is its definition:

struct timespec {
    time_t   tv_sec;        /* secondes */
    long     tv_nsec;       /* nanosecondes */
};

So the returned time in nanosecondes is: tp->tv_sec * 1e9 + tp->tv_nsec.

You can find all the possible clk_id in the man. I would recommend you to use CLOCK_MONOTONIC as it guarantees you that the time given will always be continuous, even if the time of the system is modified.

Omar
  • 943
  • 6
  • 9
0

Just call time(NULL) to get the current time and use difftime to calculate the time between two points.

#include <time.h>
// ...

time_t start = time(NULL);
// do stuff here
time_t end = time(NULL);
printf("Took %f seconds\n", difftime(end, start));
user253751
  • 57,427
  • 7
  • 48
  • 90
0

This displays start/end time stamps and calculates a delta in seconds.

#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

void print_timestamp(char *, time_t);

int main (int argc, char *argv[]) {
    time_t start = time(0);
    print_timestamp("Start:  ", start);

    sleep(2);

    time_t end   = time(0);
    print_timestamp("End:    ", end);

    double diff = difftime(end, start);

    printf("Elapsed: %5.2lf seconds\n", diff);
}

void
print_timestamp(char *msg, time_t time) {
    struct tm *tm;
    if ((tm = localtime (&time)) == NULL) {
        printf ("Error extracting time stuff\n");
        return;
    }
    printf ("%s %04d-%02d-%02d %02d:%02d:%02d\n",
        msg, 
        1900 + tm->tm_year, 
        tm->tm_mon+1, 
        tm->tm_mday,
        tm->tm_hour, 
        tm->tm_min, 
        tm->tm_sec);
}

Sample output:

Start:   2017-02-04 15:33:36  
End:     2017-02-04 15:33:38  
Elapsed: 2.00 seconds
clearlight
  • 12,255
  • 11
  • 57
  • 75
0

You may also be able to use the time command available on (at least) Unix systems.

After compiling your program, run the command like this:

# compile your code
$ gcc foo.c -o foo
# compute the time
$ time ./foo
Jules
  • 14,200
  • 13
  • 56
  • 101
-1

You can use the clock() function to record the number of ticks taken and then convert this to seconds:

#include <time.h>
#include <stdio.h>
#include <math.h>

int main () {
   clock_t start_t = clock();

   double a=0;
   for(int i=0; i< 10000000; i++) {
     a+=sqrt(a);
   }

   clock_t end_t = clock();

   double total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
   printf("Total time taken by CPU: %lf\n", total_t  );

   return(0);
}
Richard
  • 56,349
  • 34
  • 180
  • 251