#include <stdio.h>
#include <time.h>
// Use clock_gettime() - Nanosecond - Measures real elapsed time
void my_function() {
volatile long long sum = 0;
for (long long i = 0; i < 100000000; ++i) {
sum += i;
}
}
int main() {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
my_function();
clock_gettime(CLOCK_MONOTONIC, &end);
double elapsed = (end.tv_sec - start.tv_sec)
+ (end.tv_nsec - start.tv_nsec) / 1e9;
printf("Time: %f seconds\n", elapsed);
}
/*
run:
Time: 0.090654 seconds
*/