Skip to content

Instantly share code, notes, and snippets.

@CastIrony
Created April 30, 2010 05:52
Show Gist options
  • Save CastIrony/384813 to your computer and use it in GitHub Desktop.
Save CastIrony/384813 to your computer and use it in GitHub Desktop.
#ifndef TINY_PROFILER
#define TINY_PROFILER 1
// Tiny Profiler is a lightweight tool for tracking and logging multiple tasks. I use it to
// see which parts of my OpenGL scene graph take the longest to render.
//
// Because logging is relatively expensive, the TinyProfilerLog() function only logs every
// nth time it is called, where n is TINY_PROFILER_LOG_INTERVAL. Set this to 1 to log every time.
//
// TINY_PROFILER_COUNT is the maximum number of active profilers. There is no bounds-checking,
// so be very careful that this number is large enough.
//
// Usage:
//
// #include "TinyProfiler.h"
//
// TinyProfilerStart(0);
//
// // task 0 goes here
//
// TinyProfilerStop(0);
//
// TinyProfilerStart(1);
//
// // task 1 goes here
//
// TinyProfilerStop(1);
//
// TinyProfilerLog();
#define TINY_PROFILER_SHOULD_RUN 1
//Set these constants to match your own profiling needs
#define TINY_PROFILER_LOG_INTERVAL 300
#define TINY_PROFILER_COUNT 32
#define TINY_PROFILER_TIMER CFAbsoluteTimeGetCurrent()
#define TINY_PROFILER_TIME_TYPE NSTimeInterval
#define TINY_PROFILER_LOG(...) printf("%d, %d, %f5\n", __VA_ARGS__)
typedef struct
{
TINY_PROFILER_TIME_TYPE startTime;
TINY_PROFILER_TIME_TYPE totalTime;
int runCount;
}
TinyProfiler;
static TinyProfiler tinyProfilers[TINY_PROFILER_COUNT];
static int tinyProfilerLogCounter = 0;
static inline void TinyProfilerStart(int profileIndex)
{
#ifdef TINY_PROFILER_SHOULD_RUN
tinyProfilers[profileIndex].startTime = TINY_PROFILER_TIMER;
#endif
}
static inline void TinyProfilerStop(int profileIndex)
{
#ifdef TINY_PROFILER_SHOULD_RUN
tinyProfilers[profileIndex].totalTime = TINY_PROFILER_TIMER - tinyProfilers[profileIndex].startTime;
tinyProfilers[profileIndex].runCount++;
#endif
}
static inline void TinyProfilerLog()
{
#ifdef TINY_PROFILER_SHOULD_RUN
if(++tinyProfilerLogCounter != TINY_PROFILER_LOG_INTERVAL) { return; }
for(int profileIndex = 0; profileIndex < TINY_PROFILER_COUNT; profileIndex++)
{
if(!tinyProfilers[profileIndex].runCount) { continue; }
TINY_PROFILER_LOG(profileIndex, tinyProfilers[profileIndex].runCount, tinyProfilers[profileIndex].totalTime);
tinyProfilers[profileIndex].startTime = 0;
tinyProfilers[profileIndex].totalTime = 0;
tinyProfilers[profileIndex].runCount = 0;
}
tinyProfilerLogCounter = 0;
#endif
}
#endif
// ----------------------------------------------------------------------------
// "THE BEER-WARE LICENSE" (Revision 42):
// Joel Bernstein wrote this file. As long as you retain this notice you
// can do whatever you want with this stuff. If we meet some day, and you think
// this stuff is worth it, you can buy me a beer in return.
// ----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment