00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00048 #ifndef WINSTL_INCL_WINSTL_PERFORMANCE_HPP_HIGHPERFORMANCE_COUNTER
00049 #define WINSTL_INCL_WINSTL_PERFORMANCE_HPP_HIGHPERFORMANCE_COUNTER
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_HIGHPERFORMANCE_COUNTER_MAJOR 4
00053 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_HIGHPERFORMANCE_COUNTER_MINOR 0
00054 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_HIGHPERFORMANCE_COUNTER_REVISION 3
00055 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_HIGHPERFORMANCE_COUNTER_EDIT 83
00056 #endif
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #ifndef WINSTL_INCL_WINSTL_H_WINSTL
00073 # include <winstl/winstl.h>
00074 #endif
00075
00076 #if defined(STLSOFT_COMPILER_IS_MSVC) && \
00077 _MSC_VER < 1200
00078 # error winstl/performance/highperformance_counter.hpp is not compatible with Visual C++ 5.0 or earlier
00079 #endif
00080
00081 #ifndef STLSOFT_INCL_STLSOFT_UTIL_H_LIMIT_TRAITS
00082 # include <stlsoft/util/limit_traits.h>
00083 #endif
00084 #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_STATIC_INITIALISERS
00085 # include <stlsoft/util/static_initialisers.hpp>
00086 #endif
00087 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST
00088 # include <stlsoft/conversion/sap_cast.hpp>
00089 #endif
00090
00091
00092
00093
00094
00095 #ifndef _WINSTL_NO_NAMESPACE
00096 # if defined(_STLSOFT_NO_NAMESPACE) || \
00097 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00098
00099 namespace winstl
00100 {
00101 # else
00102
00103
00104 namespace stlsoft
00105 {
00106
00107 namespace winstl_project
00108 {
00109
00110 # endif
00111 #endif
00112
00113
00114
00115
00116
00117
00126 class highperformance_counter
00127 {
00128 public:
00130 typedef highperformance_counter class_type;
00131
00132 private:
00133 typedef ws_sint64_t epoch_type;
00134 public:
00138 typedef ws_sint64_t interval_type;
00139
00142 public:
00143 static void class_init()
00144 {
00145 class_type().start();
00146 }
00147 static void class_uninit()
00148 {}
00150
00151
00152 public:
00156 void start();
00160 void stop();
00161
00162
00163 public:
00167 interval_type get_period_count() const;
00171 interval_type get_seconds() const;
00175 interval_type get_milliseconds() const;
00179 interval_type get_microseconds() const;
00180
00181
00182 private:
00183 static interval_type frequency_();
00184 static interval_type query_frequency_();
00185
00186
00187 private:
00188 epoch_type m_start;
00189 epoch_type m_end;
00190 };
00191
00192
00193
00194 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00195 # if !defined(STLSOFT_COMPILER_IS_DMC) && \
00196 ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
00197 _MSC_VER >= 1200)
00198 static stlsoft_ns_qual(class_constructor)<highperformance_counter> s_highperformance_counter_class_constructor(&highperformance_counter::class_init, NULL);
00199 # endif
00200 #endif
00201
00202
00203
00204
00205
00206 #ifdef STLSOFT_UNITTEST
00207 # include "./unittest/highperformance_counter_unittest_.h"
00208 #endif
00209
00210
00211
00212
00213
00214 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00215
00216 inline highperformance_counter::interval_type highperformance_counter::query_frequency_()
00217 {
00218 interval_type frequency;
00219
00220
00221 if( !::QueryPerformanceFrequency(sap_cast<LARGE_INTEGER*>(&frequency)) ||
00222 frequency == 0)
00223 {
00224
00225
00226 frequency = stlsoft_ns_qual(limit_traits)<interval_type>::maximum();
00227 }
00228
00229 return frequency;
00230 }
00231
00232 inline highperformance_counter::interval_type highperformance_counter::frequency_()
00233 {
00234 #if !defined(STLSOFT_STRICT) && \
00235 defined(STLSOFT_COMPILER_IS_MSVC) && \
00236 _MSC_VER >= 1310
00237 # pragma warning(push)
00238 # pragma warning(disable : 4640)
00239 #endif
00240
00241 #if defined(STLSOFT_COMPILER_IS_BORLAND)
00242 interval_type s_frequency = query_frequency_();
00243 #else
00244 static interval_type s_frequency = query_frequency_();
00245 #endif
00246
00247 #if !defined(STLSOFT_STRICT) && \
00248 defined(STLSOFT_COMPILER_IS_MSVC) && \
00249 _MSC_VER >= 1310
00250 # pragma warning(pop)
00251 #endif
00252
00253 WINSTL_ASSERT(0 != s_frequency);
00254
00255 return s_frequency;
00256 }
00257
00258
00259 inline void highperformance_counter::start()
00260 {
00261 ::QueryPerformanceCounter(sap_cast<LARGE_INTEGER*>(&m_start));
00262 }
00263
00264 inline void highperformance_counter::stop()
00265 {
00266 ::QueryPerformanceCounter(sap_cast<LARGE_INTEGER*>(&m_end));
00267 }
00268
00269
00270 inline highperformance_counter::interval_type highperformance_counter::get_period_count() const
00271 {
00272 return static_cast<interval_type>(m_end - m_start);
00273 }
00274
00275 inline highperformance_counter::interval_type highperformance_counter::get_seconds() const
00276 {
00277 return get_period_count() / frequency_();
00278 }
00279
00280 inline highperformance_counter::interval_type highperformance_counter::get_milliseconds() const
00281 {
00282 highperformance_counter::interval_type result;
00283 highperformance_counter::interval_type count = get_period_count();
00284
00285 if(count < STLSOFT_GEN_SINT64_SUFFIX(0x20C49BA5E353F7))
00286 {
00287 result = (count * interval_type(1000)) / frequency_();
00288 }
00289 else
00290 {
00291 result = (count / frequency_()) * interval_type(1000);
00292 }
00293
00294 return result;
00295 }
00296
00297 inline highperformance_counter::interval_type highperformance_counter::get_microseconds() const
00298 {
00299 highperformance_counter::interval_type result;
00300 highperformance_counter::interval_type count = get_period_count();
00301
00302 if(count < STLSOFT_GEN_SINT64_SUFFIX(0x8637BD05AF6))
00303 {
00304 result = (count * interval_type(1000000)) / frequency_();
00305 }
00306 else
00307 {
00308 result = (count / frequency_()) * interval_type(1000000);
00309 }
00310
00311 return result;
00312 }
00313
00314 #endif
00315
00316
00317
00318 #ifndef _WINSTL_NO_NAMESPACE
00319 # if defined(_STLSOFT_NO_NAMESPACE) || \
00320 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00321 }
00322 # else
00323 }
00324 }
00325 # endif
00326 #endif
00327
00328
00329
00330 #endif
00331
00332