Timer 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  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.
  7. *
  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.
  12. */
  13. #ifndef OSG_TIMER
  14. #define OSG_TIMER 1
  15. #include <osg/Export>
  16. namespace osg {
  17. #if defined(_MSC_VER)
  18. typedef unsigned __int64 Timer_t;
  19. #else
  20. typedef unsigned long long Timer_t;
  21. #endif
  22. /** Timer class is used for measuring elapsed time or time between two points. */
  23. class OSG_EXPORT Timer {
  24. public:
  25. Timer();
  26. ~Timer() {}
  27. static Timer* instance();
  28. /** Get the timers tick value.*/
  29. Timer_t tick() const;
  30. /** Set the start.*/
  31. void setStartTick() { _startTick = tick(); }
  32. void setStartTick(Timer_t t) { _startTick = t; }
  33. Timer_t getStartTick() const { return _startTick; }
  34. /** Get elapsed time in seconds.*/
  35. inline double time_s() const { return delta_s(_startTick, tick()); }
  36. /** Get elapsed time in milliseconds.*/
  37. inline double time_m() const { return delta_m(_startTick, tick()); }
  38. /** Get elapsed time in microseconds.*/
  39. inline double time_u() const { return delta_u(_startTick, tick()); }
  40. /** Get elapsed time in nanoseconds.*/
  41. inline double time_n() const { return delta_n(_startTick, tick()); }
  42. /** Get the time in seconds between timer ticks t1 and t2.*/
  43. inline double delta_s( Timer_t t1, Timer_t t2 ) const { return (t2>t1) ? (double)(t2 - t1)*_secsPerTick : -(double)(t1 - t2)*_secsPerTick; }
  44. /** Get the time in milliseconds between timer ticks t1 and t2.*/
  45. inline double delta_m( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e3; }
  46. /** Get the time in microseconds between timer ticks t1 and t2.*/
  47. inline double delta_u( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e6; }
  48. /** Get the time in nanoseconds between timer ticks t1 and t2.*/
  49. inline double delta_n( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e9; }
  50. /** Get the number of seconds per tick. */
  51. inline double getSecondsPerTick() const { return _secsPerTick; }
  52. protected :
  53. Timer_t _startTick;
  54. double _secsPerTick;
  55. };
  56. /** Helper class for timing sections of code. */
  57. class ElapsedTime
  58. {
  59. public:
  60. inline ElapsedTime(double* elapsedTime, osg::Timer* timer = 0):
  61. _time(elapsedTime)
  62. {
  63. init(timer);
  64. }
  65. inline ElapsedTime(osg::Timer* timer = 0):
  66. _time(0)
  67. {
  68. init(timer);
  69. }
  70. inline ~ElapsedTime()
  71. {
  72. finish();
  73. }
  74. inline void reset()
  75. {
  76. _startTick = _timer->tick();
  77. }
  78. /** elapsed time in seconds. */
  79. inline double elapsedTime() const
  80. {
  81. return _timer->delta_s(_startTick, _timer->tick());
  82. }
  83. /** elapsed time in milliseconds. */
  84. inline double elapsedTime_m() const
  85. {
  86. return _timer->delta_m(_startTick, _timer->tick());
  87. }
  88. /** elapsed time in microseconds. */
  89. inline double elapsedTime_u() const
  90. {
  91. return _timer->delta_u(_startTick, _timer->tick());
  92. }
  93. /** elapsed time in nanoseconds. */
  94. inline double elapsedTime_n() const
  95. {
  96. return _timer->delta_n(_startTick, _timer->tick());
  97. }
  98. inline void finish()
  99. {
  100. Timer_t endTick = _timer->tick();
  101. if (_time) *_time += _timer->delta_s(_startTick, endTick);
  102. _startTick = endTick;
  103. }
  104. protected:
  105. inline void init(osg::Timer* timer)
  106. {
  107. if (timer) _timer = timer;
  108. else _timer = osg::Timer::instance();
  109. _startTick = _timer->tick();
  110. }
  111. double* _time;
  112. Timer* _timer;
  113. Timer_t _startTick;
  114. };
  115. }
  116. #endif