Last active
May 1, 2023 09:58
-
-
Save ianliu/4747098 to your computer and use it in GitHub Desktop.
A simple profiler written in C
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
// compile with: gcc -std=gnu99 example.c profiler.c -lrt | |
#include "profiler.h" | |
#include <stdio.h> | |
FILE *devnull; | |
Profiler p_total; | |
Profiler p_foo8; | |
Profiler p_foo10; | |
// Run n! times | |
void foo(int n) { | |
int i; | |
if (n == 0) | |
return; | |
for (i = 0; i < n; i++) | |
foo(n-1); | |
// Do some fancy calculations | |
int x = n*n*n % 3; | |
fprintf(devnull, "%d\n", x); | |
} | |
int main() { | |
profiler_init(&p_total, "total"); | |
profiler_start(&p_total); | |
devnull = fopen("/dev/null", "w"); | |
profiler_init(&p_foo8, "foo(8)"); | |
profiler_init(&p_foo10, "foo(10)"); | |
profiler_start(&p_foo8); | |
foo(8); | |
profiler_stop(&p_foo8); | |
profiler_start(&p_foo10); | |
foo(10); | |
profiler_stop(&p_foo10); | |
fclose(devnull); | |
profiler_stop(&p_total); | |
profiler_print(&p_total); | |
profiler_print(&p_foo8); | |
profiler_print(&p_foo10); | |
profiler_free(&p_foo8); | |
profiler_free(&p_foo10); | |
profiler_free(&p_total); | |
return 0; | |
} |
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
#include "profiler.h" | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
void profiler_init(Profiler *prof, const char *name) | |
{ | |
prof->name = strndup(name, 30); | |
prof->runs = 0; | |
prof->total = 0.0; | |
} | |
void profiler_free(Profiler *prof) | |
{ | |
free(prof->name); | |
} | |
void profiler_print(Profiler *prof) | |
{ | |
printf("%-15s % 12lld %ld\n", | |
prof->name, prof->total, prof->runs); | |
} |
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
#ifndef __PROFILER_H__ | |
#define __PROFILER_H__ | |
#include <time.h> | |
typedef unsigned long long uint64; | |
typedef struct _Profiler Profiler; | |
struct _Profiler { | |
char *name; | |
long runs; | |
uint64 total; | |
uint64 t0; | |
}; | |
void profiler_init(Profiler *prof, const char *name); | |
void profiler_free(Profiler *prof); | |
void profiler_print(Profiler *prof); | |
static __inline__ uint64 getticks(void) | |
{ | |
struct timespec tp; | |
clock_gettime(CLOCK_MONOTONIC, &tp); | |
return tp.tv_sec*1000000000 + tp.tv_nsec; | |
} | |
static __inline__ void profiler_start(Profiler *prof) | |
{ | |
prof->t0 = getticks(); | |
prof->runs++; | |
} | |
static __inline__ void profiler_stop(Profiler *prof) | |
{ | |
prof->total += getticks() - prof->t0; | |
} | |
#endif /* end of include guard: __PROFILER_H__ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment