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.

219 lines
7.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/performance/systemtime_counter.hpp
  3. *
  4. * Purpose: WinSTL system-time performance counter class.
  5. *
  6. * Created: 22nd March 2002
  7. * Updated: 13th August 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2010, 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/systemtime_counter.hpp
  40. *
  41. * \brief [C++ only] Definition of the
  42. * \link winstl::systemtime_counter systemtime_counter\endlink class
  43. * (\ref group__library__performance "Performance" Library).
  44. */
  45. #ifndef WINSTL_INCL_WINSTL_PERFORMANCE_HPP_SYSTEMTIME_COUNTER
  46. #define WINSTL_INCL_WINSTL_PERFORMANCE_HPP_SYSTEMTIME_COUNTER
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_SYSTEMTIME_COUNTER_MAJOR 4
  49. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_SYSTEMTIME_COUNTER_MINOR 0
  50. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_SYSTEMTIME_COUNTER_REVISION 4
  51. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_SYSTEMTIME_COUNTER_EDIT 45
  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. /** Performance counter that uses the Windows <code>GetSystemTime()</code> function
  80. *
  81. * \ingroup group__library__performance
  82. */
  83. class systemtime_counter
  84. {
  85. public:
  86. /// This type
  87. typedef systemtime_counter class_type;
  88. private:
  89. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  90. typedef ws_sint64_t epoch_type;
  91. #else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
  92. typedef ws_sint32_t epoch_type;
  93. #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  94. public:
  95. /// The interval type
  96. ///
  97. /// The type of the interval measurement, a 64-bit signed integer
  98. typedef epoch_type interval_type;
  99. // Construction
  100. public:
  101. systemtime_counter();
  102. // Operations
  103. public:
  104. /// Starts measurement
  105. ///
  106. /// Begins the measurement period
  107. void start();
  108. /// Ends measurement
  109. ///
  110. /// Ends the measurement period
  111. void stop();
  112. // Attributes
  113. public:
  114. /// The elapsed count in the measurement period
  115. ///
  116. /// This represents the extent, in machine-specific increments, of the measurement period
  117. interval_type get_period_count() const;
  118. /// The number of whole seconds in the measurement period
  119. ///
  120. /// This represents the extent, in whole seconds, of the measurement period
  121. interval_type get_seconds() const;
  122. /// The number of whole milliseconds in the measurement period
  123. ///
  124. /// This represents the extent, in whole milliseconds, of the measurement period
  125. interval_type get_milliseconds() const;
  126. /// The number of whole microseconds in the measurement period
  127. ///
  128. /// This represents the extent, in whole microseconds, of the measurement period
  129. interval_type get_microseconds() const;
  130. // Members
  131. private:
  132. epoch_type m_start;
  133. epoch_type m_end;
  134. };
  135. ////////////////////////////////////////////////////////////////////////////
  136. // Unit-testing
  137. #ifdef STLSOFT_UNITTEST
  138. # include "./unittest/systemtime_counter_unittest_.h"
  139. #endif /* STLSOFT_UNITTEST */
  140. ////////////////////////////////////////////////////////////////////////////
  141. // Implementation
  142. inline systemtime_counter::systemtime_counter()
  143. {
  144. // Note that the constructor does nothing, for performance reasons. Calling
  145. // any of the Attribute methods before having gone through a start()-stop()
  146. // cycle will yield undefined results.
  147. }
  148. // Operations
  149. inline void systemtime_counter::start()
  150. {
  151. ::GetSystemTimeAsFileTime(reinterpret_cast<LPFILETIME>(&m_start));
  152. }
  153. inline void systemtime_counter::stop()
  154. {
  155. ::GetSystemTimeAsFileTime(reinterpret_cast<LPFILETIME>(&m_end));
  156. }
  157. // Attributes
  158. inline systemtime_counter::interval_type systemtime_counter::get_period_count() const
  159. {
  160. return static_cast<interval_type>(m_end - m_start);
  161. }
  162. inline systemtime_counter::interval_type systemtime_counter::get_seconds() const
  163. {
  164. return get_period_count() / interval_type(10000000);
  165. }
  166. inline systemtime_counter::interval_type systemtime_counter::get_milliseconds() const
  167. {
  168. return get_period_count() / interval_type(10000);
  169. }
  170. inline systemtime_counter::interval_type systemtime_counter::get_microseconds() const
  171. {
  172. return get_period_count() / interval_type(10);
  173. }
  174. /* ////////////////////////////////////////////////////////////////////// */
  175. #ifndef _WINSTL_NO_NAMESPACE
  176. # if defined(_STLSOFT_NO_NAMESPACE) || \
  177. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  178. } // namespace winstl
  179. # else
  180. } // namespace winstl_project
  181. } // namespace stlsoft
  182. # endif /* _STLSOFT_NO_NAMESPACE */
  183. #endif /* !_WINSTL_NO_NAMESPACE */
  184. /* ////////////////////////////////////////////////////////////////////// */
  185. #endif /* !WINSTL_INCL_WINSTL_PERFORMANCE_HPP_SYSTEMTIME_COUNTER */
  186. /* ///////////////////////////// end of file //////////////////////////// */