Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8037

C/C++ • Re: Access PTP clock synchronization in C

$
0
0
Have a look at this article:
https://people.cs.rutgers.edu/~pxk/416/ ... ttime.html

I had to make a few small changes to the code:

Code:

/*        gettime - get time via clock_gettime        N.B.: OS X does not support clock_gettime        Paul Krzyzanowski*/#include <stdio.h>      /* for printf */#include <stdint.h>     /* for uint64 definition */#include <stdlib.h>     /* for exit() definition */#include <time.h>       /* for clock_gettime */#include <unistd.h>     /* for sleep() */#define BILLION 1000000000Lint localpid(void) {        static int a[9] = { 0 };        return a[0];}int main(int argc, char **argv){        uint64_t diff;        struct timespec start, end;        int i;        /* measure monotonic time */        clock_gettime(CLOCK_MONOTONIC, &start); /* mark start time */        sleep(1);       /* do stuff */        clock_gettime(CLOCK_MONOTONIC, &end);   /* mark the end time */        diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;        printf("elapsed time = %llu nanoseconds\n", (long long unsigned int) diff);        /* now re-do this and measure CPU time */        /* the time spent sleeping will not count (but there is a bit of overhead */        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);        /* mark start time */        sleep(1);       /* do stuff */        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);          /* mark the end time */        diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;        printf("elapsed process CPU time = %llu nanoseconds\n", (long long unsigned int) diff);        return 0;}
Compiled with: gcc -o hi-res-timer hi-res-time.cpp

Maybe also this on github:
https://github.com/LLNL/llnl-hires-timers/tree/master

Statistics: Posted by deepo — Sun Mar 30, 2025 7:18 pm



Viewing all articles
Browse latest Browse all 8037

Trending Articles