You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/performance/tick_counter.hpp
  3. *
  4. * Purpose: WinSTL tick-count performance counter class.
  5. *
  6. * Created: 19th October 1998
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1998-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file winstl/performance/tick_counter.hpp
  40. *
  41. * \brief [C++ only] Definition of the
  42. * \link winstl::tick_counter tick_counter\endlink class
  43. * (\ref group__library__performance "Performance" Library).
  44. */
  45. #ifndef WINSTL_INCL_WINSTL_PERFORMANCE_HPP_TICK_COUNTER
  46. #define WINSTL_INCL_WINSTL_PERFORMANCE_HPP_TICK_COUNTER
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_TICK_COUNTER_MAJOR 4
  49. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_TICK_COUNTER_MINOR 0
  50. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_TICK_COUNTER_REVISION 2
  51. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_TICK_COUNTER_EDIT 44
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  57. # include <winstl/winstl.h>
  58. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  59. /* /////////////////////////////////////////////////////////////////////////
  60. * Namespace
  61. */
  62. #ifndef _WINSTL_NO_NAMESPACE
  63. # if defined(_STLSOFT_NO_NAMESPACE) || \
  64. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  65. /* There is no stlsoft namespace, so must define ::winstl */
  66. namespace winstl
  67. {
  68. # else
  69. /* Define stlsoft::winstl_project */
  70. namespace stlsoft
  71. {
  72. namespace winstl_project
  73. {
  74. # endif /* _STLSOFT_NO_NAMESPACE */
  75. #endif /* !_WINSTL_NO_NAMESPACE */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Classes
  78. */
  79. // class tick_counter
  80. /** \brief A low-cost, low-resolution performance counter
  81. *
  82. * \ingroup group__library__performance
  83. *
  84. * This class provides low-resolution, but low-latency, performance monitoring
  85. * and is guaranteed to be meaningful on all operating systems.
  86. */
  87. class tick_counter
  88. {
  89. public:
  90. /// This type
  91. typedef tick_counter class_type;
  92. private:
  93. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  94. typedef ws_sint64_t epoch_type;
  95. #else
  96. typedef ws_sint32_t epoch_type;
  97. #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  98. public:
  99. /// \brief The interval type
  100. ///
  101. /// The type of the interval measurement, a 64-bit signed integer
  102. typedef epoch_type interval_type;
  103. // Construction
  104. public:
  105. tick_counter();
  106. // Operations
  107. public:
  108. /// \brief Starts measurement
  109. ///
  110. /// Begins the measurement period
  111. void start();
  112. /// \brief Ends measurement
  113. ///
  114. /// Ends the measurement period
  115. void stop();
  116. // Attributes
  117. public:
  118. /// \brief The elapsed count in the measurement period
  119. ///
  120. /// This represents the extent, in machine-specific increments, of the measurement period
  121. interval_type get_period_count() const;
  122. /// \brief The number of whole seconds in the measurement period
  123. ///
  124. /// This represents the extent, in whole seconds, of the measurement period
  125. interval_type get_seconds() const;
  126. /// \brief The number of whole milliseconds in the measurement period
  127. ///
  128. /// This represents the extent, in whole milliseconds, of the measurement period
  129. interval_type get_milliseconds() const;
  130. /// \brief The number of whole microseconds in the measurement period
  131. ///
  132. /// This represents the extent, in whole microseconds, of the measurement period
  133. interval_type get_microseconds() const;
  134. // Members
  135. private:
  136. ws_dword_t m_start; // start of measurement period
  137. ws_dword_t m_end; // End of measurement period
  138. };
  139. ////////////////////////////////////////////////////////////////////////////
  140. // Unit-testing
  141. #ifdef STLSOFT_UNITTEST
  142. # include "./unittest/tick_counter_unittest_.h"
  143. #endif /* STLSOFT_UNITTEST */
  144. ////////////////////////////////////////////////////////////////////////////
  145. // Implementation
  146. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  147. inline tick_counter::tick_counter()
  148. {
  149. // Note that the constructor does nothing, for performance reasons. Calling
  150. // any of the Attribute methods before having gone through a start()-stop()
  151. // cycle will yield undefined results.
  152. }
  153. // Operations
  154. inline void tick_counter::start()
  155. {
  156. m_start = ::GetTickCount();
  157. }
  158. inline void tick_counter::stop()
  159. {
  160. m_end = ::GetTickCount();
  161. }
  162. // Attributes
  163. inline tick_counter::interval_type tick_counter::get_period_count() const
  164. {
  165. return static_cast<interval_type>(m_end - m_start);
  166. }
  167. inline tick_counter::interval_type tick_counter::get_seconds() const
  168. {
  169. return get_period_count() / interval_type(1000);
  170. }
  171. inline tick_counter::interval_type tick_counter::get_milliseconds() const
  172. {
  173. return get_period_count();
  174. }
  175. inline tick_counter::interval_type tick_counter::get_microseconds() const
  176. {
  177. return get_period_count() * interval_type(1000);
  178. }
  179. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  180. /* ////////////////////////////////////////////////////////////////////// */
  181. #ifndef _WINSTL_NO_NAMESPACE
  182. # if defined(_STLSOFT_NO_NAMESPACE) || \
  183. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  184. } // namespace winstl
  185. # else
  186. } // namespace winstl_project
  187. } // namespace stlsoft
  188. # endif /* _STLSOFT_NO_NAMESPACE */
  189. #endif /* !_WINSTL_NO_NAMESPACE */
  190. /* ////////////////////////////////////////////////////////////////////// */
  191. #endif /* !WINSTL_INCL_WINSTL_PERFORMANCE_HPP_TICK_COUNTER */
  192. /* ///////////////////////////// end of file //////////////////////////// */