0ad/source/lib/timer.h
olsner 46f5c61179 Linux/GCC/glibc Compat
This was SVN commit r1532.
2004-12-18 23:30:28 +00:00

77 lines
1.6 KiB
C++
Executable File

// platform-independent high resolution timer and FPS measuring code
//
// Copyright (c) 2003 Jan Wassenberg
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// Contact info:
// Jan.Wassenberg@stud.uni-karlsruhe.de
// http://www.stud.uni-karlsruhe.de/~urkt/
#ifndef TIMER_H
#define TIMER_H
#ifdef __cplusplus
extern "C" {
#endif
// high resolution (> 1 µs) timestamp [s], starting at or near 0 s.
extern double get_time();
extern double timer_res();
// calculate fps (call once per frame)
// several smooth filters (tuned for ~100 FPS)
// => less fluctuation, but rapid tracking
extern int fps;
extern void calc_fps();
#ifdef __cplusplus
}
#endif
#include <string>
class ScopedTimer
{
double t0;
const std::string name;
public:
ScopedTimer(const char* _name)
: name(_name)
{
t0 = get_time();
}
~ScopedTimer()
{
double t1 = get_time();
double dt = t1-t0;
// assume microseconds
double scale = 1e6;
char unit = 'µ';
if(dt > 1.0)
scale = 1, unit = ' ';
// milli
else if(dt > 1e-3)
scale = 1e3, unit = 'm';
printf("TIMER %s: %g %cs\n", name.c_str(), dt*scale, unit);
}
};
#define TIMER(name) ScopedTimer name(#name);
#endif // #ifndef TIMER_H