c_cpp 用于测量性能的C ++ Timer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 用于测量性能的C ++ Timer相关的知识,希望对你有一定的参考价值。

#if _DEBUG
file://=====================================================================
=========
// WinQueryPerformance
// Outputs and resets the performance counter.
// (For multiple timers, see CPerformanceTimer)
//
// Use it like so:
//
// WinQueryPerformance( 0 );
//         do some stuff
// WinQueryPerformance( "Compiling scripts" );
//
// Sample output in debugger output window:
// Compiling scripts took .54983 seconds
//
file://=====================================================================
=========

void WinQueryPerformance( char* msg )
{
 static LARGE_INTEGER prevCounter;
 LARGE_INTEGER   currentPerformanceCounter;
 static double   frequency;

 QueryPerformanceCounter( &currentPerformanceCounter );

 if ( !frequency )
  {
  LARGE_INTEGER performanceFrequency;

  QueryPerformanceFrequency( &performanceFrequency );

  frequency = double( performanceFrequency.QuadPart );

  QueryPerformanceCounter( &prevCounter );
  }

 // Even though under VC %i64d is supposed to work, it don't, so we use
doubles.
 if ( msg )
  MATRACE( "%s %.7g took seconds\n", (const char*) msg,
ouble( currentPerformanceCounter.QuadPart - prevCounter.QuadPart ) /
frequency );

 currentPerformanceCounter = prevCounter;

 QueryPerformanceCounter( &prevCounter );

} // WinQueryPerformance


//
// The CPerformanceTimer class can be used to handle multiple timers.
// (For a simple single use timer, see WinQueryPerformance)
//
// Use it like so:
//
// {
// CPerformanceTimer timer( "Compiling scripts" );
// timer.Start( );
//         do some stuff
// timer.Report( );
// }
//
// Sample output in debugger output window:
// Compiling scripts took .54983 seconds
//
// timer.Stop() can be used to temporarily stop timer.  timer.Start() can be
used to restart.
//  timer.Report() stops the timer.
//
double CPerformanceTimer::fFrequency = 0;
int pIndentation = 0;

CPerformanceTimer::CPerformanceTimer( const char* msg )
{
 // The frequency doesn't change, so get it once.
 if ( !fFrequency )
  {
  LARGE_INTEGER performanceFrequency;

  QueryPerformanceFrequency( &performanceFrequency );

  fFrequency = double( performanceFrequency.QuadPart );
  }

 if ( strlen( msg ) >= sizeof( fMsg ) )
  WinDebugBreak( );

 strcpy( fMsg, msg );

 for ( int i = pIndentation; i; i-- )
  MATRACE( "    " );
 MATRACE( "BEGIN %s\n", (const char*) fMsg );
 pIndentation++;

 fTimer = 0.0;
 fAccum = 0.0;

} // CPerformanceTimer::CPerformanceTimer


void CPerformanceTimer::Start( )
{
 LARGE_INTEGER  currentPerformanceCounter;

 QueryPerformanceCounter( &currentPerformanceCounter );
 fTimer = (double) ( currentPerformanceCounter.QuadPart );

} // CPerformanceTimer::Start


void CPerformanceTimer::Stop( )
{
 if ( fTimer )
  {
  LARGE_INTEGER  currentPerformanceCounter;

  QueryPerformanceCounter( &currentPerformanceCounter );

  fAccum += double( currentPerformanceCounter.QuadPart - fTimer );
  fTimer = 0;
  }

} // CPerformanceTimer::Stop


void CPerformanceTimer::Report( )
{
 LARGE_INTEGER  currentPerformanceCounter;

 QueryPerformanceCounter( &currentPerformanceCounter );

 this->Stop( );

 // Even though under VC %i64d is supposed to work, it don't, so we use
doubles.
 pIndentation--;
 for ( int i = pIndentation; i; i-- )
  MATRACE( "    " );
 MATRACE( "END %s -- took %.7g seconds\n", (const char*) fMsg, fAccum /
fFrequency );

} // CPerformanceTimer::CPerformanceTimer

以上是关于c_cpp 用于测量性能的C ++ Timer的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp #/timer.c里

c_cpp 其中来自libuv的timer.c的uv_timer_start是从timer_wrap.cc中的c ++ Start函数调用的

c_cpp 提高::计时器:: auto_cpu_timer

数据结构性能分析(代码运行效率)

c_cpp C ++ TMP测量单位POC

c_cpp 测量实验室终极