Skip to content

Instantly share code, notes, and snippets.

@tdaron
Created February 5, 2025 13:43
Show Gist options
  • Save tdaron/18a1651ecfd088c349e238ea67e3292d to your computer and use it in GitHub Desktop.
Save tdaron/18a1651ecfd088c349e238ea67e3292d to your computer and use it in GitHub Desktop.
Useful macro in C to measure time some code block takes !
#define setupChrono() clock_t start; clock_t end; double cpu_time_used;
#define chrono(t, ...) start = clock();\
__VA_ARGS__;\
end = clock();\
cpu_time_used = ((double) (end - start)) * 1000 / CLOCKS_PER_SEC; \
printf("[PERFORMANCE] Name: %s | File: %s | Line: %d | Execution Time: %.3f ms\n", t,__FILE__, __LINE__, cpu_time_used);
@tdaron
Copy link
Author

tdaron commented Feb 5, 2025

Usage:

setupChrono()
chrono(
  "Name of the chrono",
  int a = 3;
  int b = 3;
  printf("%d %d \n", a, b);
)

setupChrono should be called once per code block as it defines variables going to be used by chrono.
Necessary to be able to run chrono multiple time without redefining variables x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment