00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: winstl/performance/tick_counter.hpp 00003 * 00004 * Purpose: WinSTL tick-count performance counter class. 00005 * 00006 * Created: 19th October 1998 00007 * Updated: 10th August 2009 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 1998-2009, Matthew Wilson and Synesis Software 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * - Redistributions of source code must retain the above copyright notice, this 00018 * list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00023 * any contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * ////////////////////////////////////////////////////////////////////// */ 00039 00040 00048 #ifndef WINSTL_INCL_WINSTL_PERFORMANCE_HPP_TICK_COUNTER 00049 #define WINSTL_INCL_WINSTL_PERFORMANCE_HPP_TICK_COUNTER 00050 00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00052 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_TICK_COUNTER_MAJOR 4 00053 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_TICK_COUNTER_MINOR 0 00054 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_TICK_COUNTER_REVISION 2 00055 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_TICK_COUNTER_EDIT 44 00056 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00057 00058 /* ///////////////////////////////////////////////////////////////////////// 00059 * Includes 00060 */ 00061 00062 #ifndef WINSTL_INCL_WINSTL_H_WINSTL 00063 # include <winstl/winstl.h> 00064 #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */ 00065 00066 /* ///////////////////////////////////////////////////////////////////////// 00067 * Namespace 00068 */ 00069 00070 #ifndef _WINSTL_NO_NAMESPACE 00071 # if defined(_STLSOFT_NO_NAMESPACE) || \ 00072 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00073 /* There is no stlsoft namespace, so must define ::winstl */ 00074 namespace winstl 00075 { 00076 # else 00077 /* Define stlsoft::winstl_project */ 00078 00079 namespace stlsoft 00080 { 00081 00082 namespace winstl_project 00083 { 00084 00085 # endif /* _STLSOFT_NO_NAMESPACE */ 00086 #endif /* !_WINSTL_NO_NAMESPACE */ 00087 00088 /* ///////////////////////////////////////////////////////////////////////// 00089 * Classes 00090 */ 00091 00092 // class tick_counter 00100 class tick_counter 00101 { 00102 public: 00104 typedef tick_counter class_type; 00105 00106 private: 00107 #ifdef STLSOFT_CF_64BIT_INT_SUPPORT 00108 typedef ws_sint64_t epoch_type; 00109 #else 00110 typedef ws_sint32_t epoch_type; 00111 #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */ 00112 public: 00116 typedef epoch_type interval_type; 00117 00118 // Construction 00119 public: 00120 tick_counter(); 00121 00122 // Operations 00123 public: 00127 void start(); 00131 void stop(); 00132 00133 // Attributes 00134 public: 00138 interval_type get_period_count() const; 00142 interval_type get_seconds() const; 00146 interval_type get_milliseconds() const; 00150 interval_type get_microseconds() const; 00151 00152 // Members 00153 private: 00154 ws_dword_t m_start; // start of measurement period 00155 ws_dword_t m_end; // End of measurement period 00156 }; 00157 00159 // Unit-testing 00160 00161 #ifdef STLSOFT_UNITTEST 00162 # include "./unittest/tick_counter_unittest_.h" 00163 #endif /* STLSOFT_UNITTEST */ 00164 00166 // Implementation 00167 00168 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00169 00170 inline tick_counter::tick_counter() 00171 { 00172 // Note that the constructor does nothing, for performance reasons. Calling 00173 // any of the Attribute methods before having gone through a start()-stop() 00174 // cycle will yield undefined results. 00175 } 00176 00177 // Operations 00178 inline void tick_counter::start() 00179 { 00180 m_start = ::GetTickCount(); 00181 } 00182 00183 inline void tick_counter::stop() 00184 { 00185 m_end = ::GetTickCount(); 00186 } 00187 00188 // Attributes 00189 inline tick_counter::interval_type tick_counter::get_period_count() const 00190 { 00191 return static_cast<interval_type>(m_end - m_start); 00192 } 00193 00194 inline tick_counter::interval_type tick_counter::get_seconds() const 00195 { 00196 return get_period_count() / interval_type(1000); 00197 } 00198 00199 inline tick_counter::interval_type tick_counter::get_milliseconds() const 00200 { 00201 return get_period_count(); 00202 } 00203 00204 inline tick_counter::interval_type tick_counter::get_microseconds() const 00205 { 00206 return get_period_count() * interval_type(1000); 00207 } 00208 00209 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00210 00211 /* ////////////////////////////////////////////////////////////////////// */ 00212 00213 #ifndef _WINSTL_NO_NAMESPACE 00214 # if defined(_STLSOFT_NO_NAMESPACE) || \ 00215 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00216 } // namespace winstl 00217 # else 00218 } // namespace winstl_project 00219 } // namespace stlsoft 00220 # endif /* _STLSOFT_NO_NAMESPACE */ 00221 #endif /* !_WINSTL_NO_NAMESPACE */ 00222 00223 /* ////////////////////////////////////////////////////////////////////// */ 00224 00225 #endif /* !WINSTL_INCL_WINSTL_PERFORMANCE_HPP_TICK_COUNTER */ 00226 00227 /* ///////////////////////////// end of file //////////////////////////// */