Skip to content

Instantly share code, notes, and snippets.

@r10r
Created February 22, 2014 10:08
Show Gist options
  • Save r10r/9151502 to your computer and use it in GitHub Desktop.
Save r10r/9151502 to your computer and use it in GitHub Desktop.
Time tracing header.
/*
* Simple header for tracing time.
* Include and define TRACE to enable.
*/
#ifndef _TRACE_H
#define _TRACE_H
#ifdef TRACE
#include <sys/time.h>
#include <stdio.h>
static struct timeval _TRACE_START;
static struct timeval _TRACE_END;
static double _TRACE_DIFF;
static char *_TRACE_MESSAGE;
static const char *_TRACE_BEGIN_FMT =
"~~> TRACE BEGIN [%s]\n";
static const char *_TRACE_END_FMT =
"<~~ TRACE END [%s] (%.2f ms)\n";
#define TRACE_BEGIN(message) \
gettimeofday(&_TRACE_START, NULL); \
_TRACE_MESSAGE = message; \
printf(_TRACE_BEGIN_FMT, _TRACE_MESSAGE);
#define TRACE_END \
gettimeofday(&_TRACE_END, NULL); \
_TRACE_DIFF = (_TRACE_END.tv_sec - _TRACE_START.tv_sec) * 1000.0; \
_TRACE_DIFF += (_TRACE_END.tv_usec - _TRACE_START.tv_usec) / 1000.0; \
printf(_TRACE_END_FMT, _TRACE_MESSAGE, _TRACE_DIFF);
#else
#define TRACE_BEGIN
#define TRACE_END
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment