Timer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Object.h"
  24. namespace Atomic
  25. {
  26. /// Low-resolution operating system timer.
  27. class ATOMIC_API Timer
  28. {
  29. public:
  30. /// Construct. Get the starting clock value.
  31. Timer();
  32. /// Return elapsed milliseconds and optionally reset.
  33. unsigned GetMSec(bool reset);
  34. /// Reset the timer.
  35. void Reset();
  36. private:
  37. /// Starting clock value in milliseconds.
  38. unsigned startTime_;
  39. };
  40. /// High-resolution operating system timer used in profiling.
  41. class ATOMIC_API HiresTimer
  42. {
  43. friend class Time;
  44. public:
  45. /// Construct. Get the starting high-resolution clock value.
  46. HiresTimer();
  47. /// Return elapsed microseconds and optionally reset.
  48. long long GetUSec(bool reset);
  49. /// Reset the timer.
  50. void Reset();
  51. /// Return if high-resolution timer is supported.
  52. static bool IsSupported() { return supported; }
  53. /// Return high-resolution timer frequency if supported.
  54. static long long GetFrequency() { return frequency; }
  55. private:
  56. /// Starting clock value in CPU ticks.
  57. long long startTime_;
  58. /// High-resolution timer support flag.
  59. static bool supported;
  60. /// High-resolution timer frequency.
  61. static long long frequency;
  62. };
  63. /// %Time and frame counter subsystem.
  64. class ATOMIC_API Time : public Object
  65. {
  66. ATOMIC_OBJECT(Time, Object);
  67. public:
  68. /// Construct.
  69. Time(Context* context);
  70. /// Destruct. Reset the low-resolution timer period if set.
  71. virtual ~Time();
  72. /// Begin new frame, with (last) frame duration in seconds and send frame start event.
  73. void BeginFrame(float timeStep);
  74. /// End frame. Increment total time and send frame end event.
  75. void EndFrame();
  76. /// Set the low-resolution timer period in milliseconds. 0 resets to the default period.
  77. void SetTimerPeriod(unsigned mSec);
  78. /// Return frame number, starting from 1 once BeginFrame() is called for the first time.
  79. unsigned GetFrameNumber() const { return frameNumber_; }
  80. /// Return current frame timestep as seconds.
  81. float GetTimeStep() const { return timeStep_; }
  82. /// Return current low-resolution timer period in milliseconds.
  83. unsigned GetTimerPeriod() const { return timerPeriod_; }
  84. /// Return elapsed time from program start as seconds.
  85. float GetElapsedTime();
  86. /// Get system time as milliseconds.
  87. static unsigned GetSystemTime();
  88. /// Get system time as seconds since 1.1.1970.
  89. static unsigned GetTimeSinceEpoch();
  90. /// Get a date/time stamp as a string.
  91. static String GetTimeStamp();
  92. /// Sleep for a number of milliseconds.
  93. static void Sleep(unsigned mSec);
  94. private:
  95. /// Elapsed time since program start.
  96. Timer elapsedTime_;
  97. /// Frame number.
  98. unsigned frameNumber_;
  99. /// Timestep in seconds.
  100. float timeStep_;
  101. /// Low-resolution timer period.
  102. unsigned timerPeriod_;
  103. };
  104. }