1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
22 typedef unsigned __int64 Timer_t;
24 typedef unsigned long long Timer_t;
27/** Timer class is used for measuring elapsed time or time between two points. */
28class OSG_EXPORT Timer {
35 static Timer* instance();
37 /** Get the timers tick value.*/
41 void setStartTick() { _startTick = tick(); }
42 void setStartTick(Timer_t t) { _startTick = t; }
43 Timer_t getStartTick() const { return _startTick; }
46 /** Get elapsed time in seconds.*/
47 inline double time_s() const { return delta_s(_startTick, tick()); }
49 /** Get elapsed time in milliseconds.*/
50 inline double time_m() const { return delta_m(_startTick, tick()); }
52 /** Get elapsed time in microseconds.*/
53 inline double time_u() const { return delta_u(_startTick, tick()); }
55 /** Get elapsed time in nanoseconds.*/
56 inline double time_n() const { return delta_n(_startTick, tick()); }
58 /** Get the time in seconds between timer ticks t1 and t2.*/
59 inline double delta_s( Timer_t t1, Timer_t t2 ) const { return (t2>t1) ? (double)(t2 - t1)*_secsPerTick : -(double)(t1 - t2)*_secsPerTick; }
61 /** Get the time in milliseconds between timer ticks t1 and t2.*/
62 inline double delta_m( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e3; }
64 /** Get the time in microseconds between timer ticks t1 and t2.*/
65 inline double delta_u( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e6; }
67 /** Get the time in nanoseconds between timer ticks t1 and t2.*/
68 inline double delta_n( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e9; }
70 /** Get the number of seconds per tick. */
71 inline double getSecondsPerTick() const { return _secsPerTick; }
79/** Helper class for timing sections of code. */
83 inline ElapsedTime(double* elapsedTime, osg::Timer* timer = 0):
89 inline ElapsedTime(osg::Timer* timer = 0):
102 _startTick = _timer->tick();
105 /** elapsed time in seconds. */
106 inline double elapsedTime() const
108 return _timer->delta_s(_startTick, _timer->tick());
111 /** elapsed time in milliseconds. */
112 inline double elapsedTime_m() const
114 return _timer->delta_m(_startTick, _timer->tick());
117 /** elapsed time in microseconds. */
118 inline double elapsedTime_u() const
120 return _timer->delta_u(_startTick, _timer->tick());
123 /** elapsed time in nanoseconds. */
124 inline double elapsedTime_n() const
126 return _timer->delta_n(_startTick, _timer->tick());
131 Timer_t endTick = _timer->tick();
132 if (_time) *_time += _timer->delta_s(_startTick, endTick);
133 _startTick = endTick;
138 inline void init(osg::Timer* timer)
140 if (timer) _timer = timer;
141 else _timer = osg::Timer::instance();
143 _startTick = _timer->tick();