1
0
forked from 0ad/0ad
0ad/source/lib/timer.h
prefect b67b023513 Add more verbosity to the shutdown sequence.
Shutdown doesn't look clean on Linux, and this might help highlight
current and future problems. Besides, we don't want shutdown to take
ages, do we? ;)

This was SVN commit r2801.
2005-09-29 02:30:30 +00:00

88 lines
1.9 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
#include <string>
#include "debug.h" // debug_printf
#ifdef __cplusplus
extern "C" {
#endif
// high resolution (> 1 us) timestamp [s], starting at or near 0 s.
extern double get_time(void);
extern double timer_res(void);
// 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(void);
#ifdef __cplusplus
}
#endif
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 = 'u';
if(dt > 1.0)
scale = 1, unit = ' ';
// milli
else if(dt > 1e-3)
scale = 1e3, unit = 'm';
debug_printf("TIMER %s: %g %cs\n", name.c_str(), dt*scale, unit);
}
// no copy ctor, since some members are const
private:
ScopedTimer& operator=(const ScopedTimer&);
};
#define TIMER(name) ScopedTimer st##name##instance(#name)
// Cheat a bit to make things slightly easier on the user
#define TIMER_START(name) { ScopedTimer __timer( name )
#define TIMER_END(name) }
#endif // #ifndef TIMER_H