Skip to content

Instantly share code, notes, and snippets.

@bearice
Created May 6, 2020 17:43
Show Gist options
  • Select an option

  • Save bearice/22fb2fa11db4e39049734b7c4a911a9c to your computer and use it in GitHub Desktop.

Select an option

Save bearice/22fb2fa11db4e39049734b7c4a911a9c to your computer and use it in GitHub Desktop.

Revisions

  1. bearice created this gist May 6, 2020.
    44 changes: 44 additions & 0 deletions time.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #include <stddef.h>
    #include <stdio.h>
    #include <time.h>
    #include <sys/time.h>

    struct timespec lmt1 = {0};
    struct timespec lmt2 = {0};
    struct timespec lmt3 = {0};
    struct timeval lrt = {0};

    void ptm(){
    /* get monotonic clock time */
    struct timespec monotime1;
    clock_gettime(CLOCK_MONOTONIC, &monotime1);

    struct timespec monotime2;
    clock_gettime(CLOCK_MONOTONIC_RAW, &monotime2);

    struct timespec monotime3;
    clock_gettime(CLOCK_MONOTONIC_COARSE, &monotime3);

    /* get realtime clock time for comparison */
    struct timeval realtime;
    gettimeofday(&realtime,NULL);

    printf("Mono Time = %ld.%09ld\n", monotime1.tv_sec-lmt1.tv_sec, monotime1.tv_nsec-lmt1.tv_nsec);
    printf("Mono Fast = %ld.%09ld\n", monotime2.tv_sec-lmt2.tv_sec, monotime2.tv_nsec-lmt2.tv_nsec);
    printf("Mono Choa = %ld.%09ld\n", monotime3.tv_sec-lmt3.tv_sec, monotime3.tv_nsec-lmt3.tv_nsec);
    printf("Real Time = %ld.%06ld\n", realtime.tv_sec-lrt.tv_sec, realtime.tv_usec-lrt.tv_usec);
    puts("==============");

    lmt1 = monotime1;
    lmt2 = monotime2;
    lmt3 = monotime3;
    lrt = realtime;
    }

    int main(void) {
    while(42){
    ptm();
    sleep(1);
    }
    return 0;
    }