| Snippet | Performance | C# | Tools | .NET |
SummaryI acquired the StopWatch class and have used it extensively for measuring performance of new code blocks and algorithms. It measures time in 10ths of milliseconds.
The DateTime class will only measure time to the nearest 10ms, and I was never sure I could trust even that. The StopWatch class has been excellent.
|
Example: How to use the StopWatch Classpublic static void TestStopWatch()
{
StopWatch sw = new StopWatch();
int temp = 0;
int repetitions = 1000000;
sw.Reset();
for (int i=0; i<repetitions; i++)
temp++;
long time = sw.Peek();
Console.WriteLine("Time = " + time/10.0 + " milliseconds.");
}
|
| If you run the previous method you'll get the folloing output: |
The C# StopWatch Class:
using System;
using System.Runtime.InteropServices;
namespace Cambia.CoreLib
{
/// <summary>
///
///
/// </summary>
public sealed class StopWatch
{
/// <summary>
///
///
/// </summary>
/// <param name="x">
///
///
/// </param>
/// <returns>
///
///
/// </returns>
[DllImport("kernel32.dll")]
extern static int QueryPerformanceCounter(ref long x);
/// <summary>
///
///
///
///
/// </summary>
/// <param name="x">
///
///
///
///
///
/// </param>
/// <returns>
///
///
///
/// </returns>
[DllImport("kernel32.dll")]
extern static int QueryPerformanceFrequency(ref long x);
/// <summary>
///
/// </summary>
/// <exception cref="NotSupportedException">
///
///
/// </exception>
public StopWatch()
{
Frequency = GetFrequency();
Reset();
}
/// <summary>
///
///
/// </summary>
/// <exception cref="NotSupportedException">
///
///
/// </exception>
public void Reset()
{
StartTime = GetValue();
}
/// <summary>
///
///
/// </summary>
/// <remarks>
///
///
///
/// </remarks>
/// <returns>
///
///
/// </returns>
/// <exception cref="NotSupportedException">
///
/// </exception>
public long Peek()
{
return (long)(((GetValue() - StartTime)
/ (double)Frequency) * 10000);
}
/// <summary>
///
///
/// </summary>
/// <exception cref="NotSupportedException">
///
///
/// </exception>
/// <returns>
///
///
/// </returns>
private long GetValue()
{
long ret = 0;
if (QueryPerformanceCounter(ref ret) == 0)
throw new NotSupportedException(
"Error while querying "
+ "the high-resolution performance counter.");
return ret;
}
/// <summary>
///
///
///
/// </summary>
/// <exception cref="NotSupportedException">
///
///
/// </exception>
/// <returns>
///
///
/// </returns>
private long GetFrequency()
{
long ret = 0;
if (QueryPerformanceFrequency(ref ret) == 0)
throw new NotSupportedException(
"Error while querying "
+ "the performance counter frequency.");
return ret;
}
/// <summary>
///
/// </summary>
/// <value>
///
/// </value>
private long StartTime
{
get
{
return m_StartTime;
}
set
{
m_StartTime = value;
}
}
/// <summary>
///
///
/// </summary>
/// <value>
///
///
/// </value>
private long Frequency
{
get
{
return m_Frequency;
}
set
{
m_Frequency = value;
}
}
#region -- Private Variables --
/// <summary>
///
/// </summary>
private long m_StartTime;
/// <summary>
///
/// </summary>
private long m_Frequency;
#endregion
}
} |
|