Skip to content

Instantly share code, notes, and snippets.

@godoio
Last active July 20, 2022 15:16
Show Gist options
  • Save godoio/0643e01b5e850a25aa471fe459277262 to your computer and use it in GitHub Desktop.
Save godoio/0643e01b5e850a25aa471fe459277262 to your computer and use it in GitHub Desktop.
A code to measure the speed of your code in nano seconds
// This codelet does not give you the exact time your C code ran for
// but a number signifying the nano seconds your C code ran for which
// includes your cpu speed.
#include<stdio.h>
#include<sys/time.h>
#include <inttypes.h>
int main() {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
// Write your code here or in any function and declare here
clock_gettime(CLOCK_MONOTONIC, &end);
uint64_t delta_us = end.tv_nsec - start.tv_nsec;
printf("Time spent on the code %" PRIu64 "ns", delta_us);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment