Skip to content

Instantly share code, notes, and snippets.

@pegvin
Last active February 2, 2023 19:56
Show Gist options
  • Save pegvin/c8f385d17f0f09dd339981b73584abf5 to your computer and use it in GitHub Desktop.
Save pegvin/c8f385d17f0f09dd339981b73584abf5 to your computer and use it in GitHub Desktop.
Simple Macro Based Function Profiler in C
#include "simple_profiler.h"
#ifdef LINUX
#include <unistd.h>
#endif
#ifdef WINDOWS
#include <windows.h>
#endif
void _sleepms(int ms) {
#ifdef LINUX
usleep(sleepMs * 1000);
#endif
#ifdef WINDOWS
Sleep(ms);
#endif
}
int main(void) {
MEASURE_FUNC_TIME_START();
_sleepms(111);
MEASURE_FUNC_TIME_END("_sleepms(111);");
return 0;
}
#include <stdio.h>
#include <time.h>
clock_t __clockTimeRecorded = 0;
#define MEASURE_FUNC_TIME_START() __clockTimeRecorded = clock()
#define MEASURE_FUNC_TIME_END(strPrefix) \
__clockTimeRecorded = clock() - __clockTimeRecorded; \
printf("%s took %fms\n", strPrefix, ((double)__clockTimeRecorded/CLOCKS_PER_SEC) * 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment