HighRezTimer.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Assert.h>
  7. #include <AnKi/Util/StdTypes.h>
  8. namespace anki {
  9. /// @addtogroup util_time
  10. /// @{
  11. /// High resolution timer. All time in seconds
  12. class HighRezTimer
  13. {
  14. public:
  15. /// Start the timer
  16. void start()
  17. {
  18. m_startTime = getCurrentTime();
  19. m_stopTime = 0.0;
  20. }
  21. /// Stop the timer
  22. void stop()
  23. {
  24. ANKI_ASSERT(m_startTime != 0.0);
  25. ANKI_ASSERT(m_stopTime == 0.0);
  26. m_stopTime = getCurrentTime();
  27. }
  28. /// Get the time elapsed between start and stop (if its stopped) or between start and the current time.
  29. Second getElapsedTime() const
  30. {
  31. return (m_stopTime == 0.0) ? getCurrentTime() - m_startTime : m_stopTime - m_startTime;
  32. }
  33. /// Get the current date's seconds
  34. static Second getCurrentTime();
  35. /// Get the current date's mili seconds
  36. static U64 getCurrentTimeMs();
  37. /// Get the current date's micro seconds
  38. static U64 getCurrentTimeUs();
  39. /// Micro sleep. The resolution is in nanoseconds.
  40. static void sleep(Second seconds);
  41. private:
  42. Second m_startTime = 0.0;
  43. Second m_stopTime = 0.0;
  44. };
  45. /// @}
  46. } // end namespace anki