stopwatch.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/stopwatch.h
  3. // Purpose: wxStopWatch and global time-related functions
  4. // Author: Julian Smart (wxTimer), Sylvain Bougnoux (wxStopWatch),
  5. // Vadim Zeitlin (time functions, current wxStopWatch)
  6. // Created: 26.06.03 (extracted from wx/timer.h)
  7. // Copyright: (c) 1998-2003 Julian Smart, Sylvain Bougnoux
  8. // (c) 2011 Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_STOPWATCH_H_
  12. #define _WX_STOPWATCH_H_
  13. #include "wx/defs.h"
  14. #include "wx/longlong.h"
  15. // Time-related functions are also available via this header for compatibility
  16. // but you should include wx/time.h directly if you need only them and not
  17. // wxStopWatch itself.
  18. #include "wx/time.h"
  19. // ----------------------------------------------------------------------------
  20. // wxStopWatch: measure time intervals with up to 1ms resolution
  21. // ----------------------------------------------------------------------------
  22. #if wxUSE_STOPWATCH
  23. class WXDLLIMPEXP_BASE wxStopWatch
  24. {
  25. public:
  26. // ctor starts the stop watch
  27. wxStopWatch() { m_pauseCount = 0; Start(); }
  28. // Start the stop watch at the moment t0 expressed in milliseconds (i.e.
  29. // calling Time() immediately afterwards returns t0). This can be used to
  30. // restart an existing stopwatch.
  31. void Start(long t0 = 0);
  32. // pause the stop watch
  33. void Pause()
  34. {
  35. if ( m_pauseCount++ == 0 )
  36. m_elapsedBeforePause = GetCurrentClockValue() - m_t0;
  37. }
  38. // resume it
  39. void Resume()
  40. {
  41. wxASSERT_MSG( m_pauseCount > 0,
  42. wxT("Resuming stop watch which is not paused") );
  43. if ( --m_pauseCount == 0 )
  44. {
  45. DoStart();
  46. m_t0 -= m_elapsedBeforePause;
  47. }
  48. }
  49. // Get elapsed time since the last Start() in microseconds.
  50. wxLongLong TimeInMicro() const;
  51. // get elapsed time since the last Start() in milliseconds
  52. long Time() const { return (TimeInMicro()/1000).ToLong(); }
  53. private:
  54. // Really starts the stop watch. The initial time is set to current clock
  55. // value.
  56. void DoStart();
  57. // Returns the current clock value in its native units.
  58. wxLongLong GetCurrentClockValue() const;
  59. // Return the frequency of the clock used in its ticks per second.
  60. wxLongLong GetClockFreq() const;
  61. // The clock value when the stop watch was last started. Its units vary
  62. // depending on the platform.
  63. wxLongLong m_t0;
  64. // The elapsed time as of last Pause() call (only valid if m_pauseCount >
  65. // 0) in the same units as m_t0.
  66. wxLongLong m_elapsedBeforePause;
  67. // if > 0, the stop watch is paused, otherwise it is running
  68. int m_pauseCount;
  69. };
  70. #endif // wxUSE_STOPWATCH
  71. #if wxUSE_LONGLONG && WXWIN_COMPATIBILITY_2_6
  72. // Starts a global timer
  73. // -- DEPRECATED: use wxStopWatch instead
  74. wxDEPRECATED( void WXDLLIMPEXP_BASE wxStartTimer() );
  75. // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
  76. // -- DEPRECATED: use wxStopWatch instead
  77. wxDEPRECATED( long WXDLLIMPEXP_BASE wxGetElapsedTime(bool resetTimer = true) );
  78. #endif // wxUSE_LONGLONG && WXWIN_COMPATIBILITY_2_6
  79. #endif // _WX_STOPWATCH_H_