使用API实现1/1000秒的性能测试类
//
MbUnit Test Framework
//
// Copyright (c) 2004 Jonathan de Halleux
//
// This software is provided 'as-is', without any express or implied warranty.
//
// In no event will the authors be held liable for any damages arising from
// the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment in the product
// documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must
// not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
// MbUnit HomePage: http://mbunit.tigris.org
// Author: Jonathan de Halleux
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;
namespace MbUnit.Core.Monitoring
{
/**/ /// <summary>
/// A high performance timer
/// </summary>
/// <remarks>
/// High Precision Timer based on Win32 methods.
/// </remarks>
/// <example>
/// This example times the execution of a method:
/// <code>
/// TimeMonitor timer = new TimeMonitor();
/// timer.Start();
/// // execute code
/// timer.Stop();
///
/// Console.WriteLine("Duration: {0}",timer.Duration);
/// </example>
public class TimeMonitor
{
[DllImport( " Kernel32.dll " )]
private static extern bool QueryPerformanceCounter( out long lpPerformanceCount);
[DllImport( " Kernel32.dll " )]
private static extern bool QueryPerformanceFrequency( out long lpFrequency);
private long startTime, stopTime;
private long now;
private long freq;
/**/ /// <summary> Default constructor </summary>
/// <remarks> Initializes the timer. </remarks>
public TimeMonitor()
{
startTime = 0 ;
stopTime = 0 ;
if (QueryPerformanceFrequency( out freq) == false )
{
// high-performance counter not supported
throw new Win32Exception();
}
}
/**/ /// <summary> Gets the clock frequency </summary>
/// <value> Clock frequency </value>
public long Frequency
{
get
{
return this .freq;
}
}
/**/ /// <summary> Starts the timer </summary>
/// <remarks> Resets the duration and starts the timer </remarks>
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep( 0 );
QueryPerformanceCounter( out startTime);
}
/**/ /// <summary> Stops the timer </summary>
/// <remarks> Stops the timer </remarks>
public void Stop()
{
QueryPerformanceCounter( out stopTime);
}
/**/ /// <summary> Gets the current duration value without stopping the timer </summary>
/// <value> Current duration value </value>
public double Now
{
get
{
QueryPerformanceCounter( out now);
return ( double )(now - startTime) / ( double ) freq;
}
}
/**/ /// <summary> Gets the timed duration value in seconds </summary>
/// <value> Timer duration </value>
public double Duration
{
get
{
return ( double )(stopTime - startTime) / ( double ) freq;
}
}
}
}
//
// Copyright (c) 2004 Jonathan de Halleux
//
// This software is provided 'as-is', without any express or implied warranty.
//
// In no event will the authors be held liable for any damages arising from
// the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment in the product
// documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must
// not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
// MbUnit HomePage: http://mbunit.tigris.org
// Author: Jonathan de Halleux
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;
namespace MbUnit.Core.Monitoring
{
/**/ /// <summary>
/// A high performance timer
/// </summary>
/// <remarks>
/// High Precision Timer based on Win32 methods.
/// </remarks>
/// <example>
/// This example times the execution of a method:
/// <code>
/// TimeMonitor timer = new TimeMonitor();
/// timer.Start();
/// // execute code
/// timer.Stop();
///
/// Console.WriteLine("Duration: {0}",timer.Duration);
/// </example>
public class TimeMonitor
{
[DllImport( " Kernel32.dll " )]
private static extern bool QueryPerformanceCounter( out long lpPerformanceCount);
[DllImport( " Kernel32.dll " )]
private static extern bool QueryPerformanceFrequency( out long lpFrequency);
private long startTime, stopTime;
private long now;
private long freq;
/**/ /// <summary> Default constructor </summary>
/// <remarks> Initializes the timer. </remarks>
public TimeMonitor()
{
startTime = 0 ;
stopTime = 0 ;
if (QueryPerformanceFrequency( out freq) == false )
{
// high-performance counter not supported
throw new Win32Exception();
}
}
/**/ /// <summary> Gets the clock frequency </summary>
/// <value> Clock frequency </value>
public long Frequency
{
get
{
return this .freq;
}
}
/**/ /// <summary> Starts the timer </summary>
/// <remarks> Resets the duration and starts the timer </remarks>
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep( 0 );
QueryPerformanceCounter( out startTime);
}
/**/ /// <summary> Stops the timer </summary>
/// <remarks> Stops the timer </remarks>
public void Stop()
{
QueryPerformanceCounter( out stopTime);
}
/**/ /// <summary> Gets the current duration value without stopping the timer </summary>
/// <value> Current duration value </value>
public double Now
{
get
{
QueryPerformanceCounter( out now);
return ( double )(now - startTime) / ( double ) freq;
}
}
/**/ /// <summary> Gets the timed duration value in seconds </summary>
/// <value> Timer duration </value>
public double Duration
{
get
{
return ( double )(stopTime - startTime) / ( double ) freq;
}
}
}
}
c++版的:
High
-
resolution timer
for
timing code fragments
Timing of code fragments is simple:
record the start time
execute the code
record the end time
calucate the difference between end and start
For timing exuction time of pieces of code you need a high - resolution timer. On Windows, Windows CE and Pocket PC / Smartphone (which are Windows CE variations) you can use QueryPerformanceCounter and QueryPerformanceFrequence API calls.
Here ' s a simple implementation in C:
typedef struct prof_timer_t {
LARGE_INTEGER time_start;
LARGE_INTEGER time_stop;
} prof_timer_t;
void prof_timer_start(prof_timer_t * timer) {
QueryPerformanceCounter(&timer->time_start);
}
void prof_timer_stop(prof_timer_t * timer) {
QueryPerformanceCounter(&timer->time_stop);
}
double prof_timer_get_duration_in_secs(prof_timer_t * timer) {
LARGE_INTEGER freq;
double duration;
QueryPerformanceFrequency(&freq);
duration = (double)(timer->time_stop.QuadPart-timer->time_start.QuadPart)/(double)freq.QuadPart;
return duration;
}
And in C ++ :
// very simple, high-precision (at least in theory) timer for timing API calls
struct ProfTimer {
void Start(void) {
QueryPerformanceCounter(&mTimeStart);
};
void Stop(void) {
QueryPerformanceCounter(&mTimeStop);
};
double GetDurationInSecs(void)
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
double duration = (double)(mTimeStop.QuadPart-mTimeStart.QuadPart)/(double)freq.QuadPart;
return duration;
}
LARGE_INTEGER mTimeStart;
LARGE_INTEGER mTimeStop;
} ;
And here ' s an example of using the C++ version:
ProfTimer t;
t.Start();
foo();
t.Stop();
double dur = t.GetDurationInSecs();
printf( " executing foo() took %f seconds\n " , dur);
Timing of code fragments is simple:
record the start time
execute the code
record the end time
calucate the difference between end and start
For timing exuction time of pieces of code you need a high - resolution timer. On Windows, Windows CE and Pocket PC / Smartphone (which are Windows CE variations) you can use QueryPerformanceCounter and QueryPerformanceFrequence API calls.
Here ' s a simple implementation in C:
typedef struct prof_timer_t {
LARGE_INTEGER time_start;
LARGE_INTEGER time_stop;
} prof_timer_t;
void prof_timer_start(prof_timer_t * timer) {
QueryPerformanceCounter(&timer->time_start);
}
void prof_timer_stop(prof_timer_t * timer) {
QueryPerformanceCounter(&timer->time_stop);
}
double prof_timer_get_duration_in_secs(prof_timer_t * timer) {
LARGE_INTEGER freq;
double duration;
QueryPerformanceFrequency(&freq);
duration = (double)(timer->time_stop.QuadPart-timer->time_start.QuadPart)/(double)freq.QuadPart;
return duration;
}
And in C ++ :
// very simple, high-precision (at least in theory) timer for timing API calls
struct ProfTimer {
void Start(void) {
QueryPerformanceCounter(&mTimeStart);
};
void Stop(void) {
QueryPerformanceCounter(&mTimeStop);
};
double GetDurationInSecs(void)
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
double duration = (double)(mTimeStop.QuadPart-mTimeStart.QuadPart)/(double)freq.QuadPart;
return duration;
}
LARGE_INTEGER mTimeStart;
LARGE_INTEGER mTimeStop;
} ;
And here ' s an example of using the C++ version:
ProfTimer t;
t.Start();
foo();
t.Stop();
double dur = t.GetDurationInSecs();
printf( " executing foo() took %f seconds\n " , dur);