Performance measure

.Net Framework 1.1 doesn’t have cool System.Diagnostic.Stopwatch class to measure performance.
So, i write my own performance measure util for .Net Framework 1.1
 
PerformanceTimer:
 
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
 

namespace Utils

{
public class PerformanceTimer : IDisposable
{
[DllImport("KERNEL32")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
 
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
 
private string _description;
private long _start;
private long _stop;
private long _frequency;
 
public PerformanceTimer(string descripton)
{
_description = descripton;
Prepare();
if (QueryPerformanceFrequency(out _frequency) == false)
{
// Frequency not supported
throw new Win32Exception();
}
QueryPerformanceCounter(out _start);
}
 
public void Dispose()
{
QueryPerformanceCounter(out _stop);
Console.WriteLine("{0} elapsed {1, 6:###.00} milliseconds", _description, (double)((_stop – _start)* 1000) / (double) _frequency);
}
private static void Prepare()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
}
 

Example:

 
public static void Main()
{
using (PerformanceTimer a = new PerformanceTimer("test"))
{
System.Threading.Thread.Sleep(1000);
}
}

Results:

test: elapsed 1000,72 milliseconds

see also

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s