Last active
July 20, 2022 15:16
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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