📄 performance_counter.hpp
字号:
_MSC_VER >= 1200)
static stlsoft_ns_qual(class_constructor)<performance_counter> s_performance_counter_class_constructor(&performance_counter::class_init, NULL);
# endif /* compiler */
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Unit-testing
*/
#ifdef STLSOFT_UNITTEST
# include "./unittest/performance_counter_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Implementation
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
inline /* static */ performance_counter::interval_type performance_counter::query_frequency_()
{
interval_type frequency;
// If no high-performance counter is available ...
if( !::QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&frequency)) ||
frequency == 0)
{
// ... then set the divisor to be the frequency for GetTickCount(), which is
// 1000 since it returns intervals in milliseconds.
frequency = 1000;
}
return frequency;
}
inline /* static */ performance_counter::interval_type performance_counter::frequency_()
{
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER >= 1310
// Safe to suppress these warnings, because race-conditions are benign here
# pragma warning(push)
# pragma warning(disable : 4640)
#endif /* compiler */
#if defined(STLSOFT_COMPILER_IS_BORLAND)
interval_type s_frequency = query_frequency_();
#else /* ? compiler */
static interval_type s_frequency = query_frequency_();
#endif /* compiler */
WINSTL_ASSERT(0 != s_frequency);
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER >= 1310
# pragma warning(pop)
#endif /* compiler */
return s_frequency;
}
inline /* static */ void performance_counter::qpc_(epoch_type &epoch)
{
static_cast<void>(::QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&epoch)));
}
inline /* static */ void performance_counter::gtc_(epoch_type &epoch)
{
epoch = static_cast<ws_sint32_t>(::GetTickCount());
}
inline /* static */ performance_counter::measure_fn_type performance_counter::get_measure_fn_()
{
measure_fn_type fn;
epoch_type frequency;
if(QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&frequency)))
{
fn = qpc_;
}
else
{
fn = gtc_;
}
return fn;
}
inline /* static */ void performance_counter::measure_(epoch_type &epoch)
{
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER >= 1310
// Safe to suppress these warnings, because race-conditions are benign here
# pragma warning(push)
# pragma warning(disable : 4640)
#endif /* compiler */
static measure_fn_type fn = get_measure_fn_();
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER >= 1310
# pragma warning(pop)
#endif /* compiler */
fn(epoch);
}
// Operations
inline void performance_counter::start()
{
measure_(m_start);
}
inline void performance_counter::stop()
{
measure_(m_end);
}
inline void performance_counter::restart()
{
measure_(m_start);
m_end = m_start;
}
// Attributes
inline /* static */ performance_counter::epoch_type performance_counter::get_epoch()
{
epoch_type epoch;
measure_(epoch);
return epoch;
}
inline /* static */ performance_counter::interval_type performance_counter::get_seconds(performance_counter::epoch_type start, performance_counter::epoch_type end)
{
interval_type period_count = static_cast<interval_type>(end - start);
return period_count / frequency_();
}
inline /* static */ performance_counter::interval_type performance_counter::get_milliseconds(performance_counter::epoch_type start, performance_counter::epoch_type end)
{
interval_type result;
interval_type count = static_cast<interval_type>(end - start);
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
if(count < STLSOFT_GEN_SINT64_SUFFIX(0x20C49BA5E353F7))
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
if(count < interval_type(0x20C49B, 0xA5E353F7))
#endif /* !STLSOFT_CF_64BIT_INT_SUPPORT */
{
result = (count * interval_type(1000)) / frequency_();
}
else
{
result = (count / frequency_()) * interval_type(1000);
}
return result;
}
inline /* static */ performance_counter::interval_type performance_counter::get_microseconds(performance_counter::epoch_type start, performance_counter::epoch_type end)
{
interval_type result;
interval_type count = static_cast<interval_type>(end - start);
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
if(count < STLSOFT_GEN_SINT64_SUFFIX(0x8637BD05AF6))
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
if(count < interval_type(0x863, 0x7BD05AF6))
#endif /* !STLSOFT_CF_64BIT_INT_SUPPORT */
{
result = (count * interval_type(1000000)) / frequency_();
}
else
{
result = (count / frequency_()) * interval_type(1000000);
}
return result;
}
inline performance_counter::interval_type performance_counter::get_period_count() const
{
return static_cast<interval_type>(m_end - m_start);
}
inline performance_counter::interval_type performance_counter::get_seconds() const
{
return get_period_count() / frequency_();
}
inline performance_counter::interval_type performance_counter::get_milliseconds() const
{
interval_type result;
interval_type count = get_period_count();
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
if(count < STLSOFT_GEN_SINT64_SUFFIX(0x20C49BA5E353F7))
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
if(count < interval_type(0x20C49B, 0xA5E353F7))
#endif /* !STLSOFT_CF_64BIT_INT_SUPPORT */
{
result = (count * interval_type(1000)) / frequency_();
}
else
{
result = (count / frequency_()) * interval_type(1000);
}
return result;
}
inline performance_counter::interval_type performance_counter::get_microseconds() const
{
interval_type result;
interval_type count = get_period_count();
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
if(count < STLSOFT_GEN_SINT64_SUFFIX(0x8637BD05AF6))
#else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
if(count < interval_type(0x863, 0x7BD05AF6))
#endif /* !STLSOFT_CF_64BIT_INT_SUPPORT */
{
result = (count * interval_type(1000000)) / frequency_();
}
else
{
result = (count / frequency_()) * interval_type(1000000);
}
return result;
}
inline performance_counter::interval_type performance_counter::stop_get_period_count_and_restart()
{
stop();
interval_type interval = get_period_count();
m_start = m_end;
return interval;
}
inline performance_counter::interval_type performance_counter::stop_get_seconds_and_restart()
{
stop();
interval_type interval = get_seconds();
m_start = m_end;
return interval;
}
inline performance_counter::interval_type performance_counter::stop_get_milliseconds_and_restart()
{
stop();
interval_type interval = get_milliseconds();
m_start = m_end;
return interval;
}
inline performance_counter::interval_type performance_counter::stop_get_microseconds_and_restart()
{
stop();
interval_type interval = get_microseconds();
m_start = m_end;
return interval;
}
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !WINSTL_INCL_WINSTL_PERFORMANCE_HPP_PERFORMANCE_COUNTER */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -