Skip to content

Instantly share code, notes, and snippets.

@pbosetti
Created April 15, 2022 12:35
Show Gist options
  • Save pbosetti/d2c0a9d80d9b9507e170b495ecb33b50 to your computer and use it in GitHub Desktop.
Save pbosetti/d2c0a9d80d9b9507e170b495ecb33b50 to your computer and use it in GitHub Desktop.
TicToc timing utility for C
/*
_____ _ _____
|_ _(_) __|_ _|__ ___
| | | |/ __|| |/ _ \ / __|
| | | | (__ | | (_) | (__
|_| |_|\___||_|\___/ \___|
* timing utility
//
#include <time.h>
// Measuring execution time intervals: each time it is called, it returns
// the delay to the previous call. Remember to call it once for initializing
// internal timer.
// It is intended to be defined in a header file,
// declared as static inline, so calling it is quick (if it gets actually inlined) and
// it is file-local (different files, different timers).
static inline double tictoc() {
static struct timespec ts = {.tv_sec = 0, .tv_nsec = 0};
double prev = ts.tv_sec + ts.tv_nsec / 1.0E9;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec / 1.0E9 - prev;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment