// program.cs
// PerfTimer 及 测试程序
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;
namespace TestPerformance
{
internal sealed class PerfTimer
{
[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 freq;
// Constructor
public PerfTimer()
{
startTime = 0;
stopTime = 0;
if (QueryPerformanceFrequency(out freq) == false)
{
// high-performance counter not supported
throw new Win32Exception();
}
}
// Start the timer
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
}
// Stop the timer
public void Stop()
{
QueryPerformanceCounter(out stopTime);
}
// Returns the duration of the timer (in seconds)
public double Duration
{
get
{
return (double)(stopTime - startTime) / (double)freq;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to continue");
Console.ReadKey();
PerfTimer pt = new PerfTimer(); // create a new PerfTimer object
pt.Start(); // start the timer
for(int i=0; i<10000; i++)
{
string s = string.Format("This is test round {0}", i);//"This is test round "+i.ToString();
Console.WriteLine(s);
}
pt.Stop();
Console.WriteLine("Duration: {0} second(s)"n", pt.Duration); // print the duration of the timed code
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;
namespace TestPerformance
{
internal sealed class PerfTimer
{
[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 freq;
// Constructor
public PerfTimer()
{
startTime = 0;
stopTime = 0;
if (QueryPerformanceFrequency(out freq) == false)
{
// high-performance counter not supported
throw new Win32Exception();
}
}
// Start the timer
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
}
// Stop the timer
public void Stop()
{
QueryPerformanceCounter(out stopTime);
}
// Returns the duration of the timer (in seconds)
public double Duration
{
get
{
return (double)(stopTime - startTime) / (double)freq;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to continue");
Console.ReadKey();
PerfTimer pt = new PerfTimer(); // create a new PerfTimer object
pt.Start(); // start the timer
for(int i=0; i<10000; i++)
{
string s = string.Format("This is test round {0}", i);//"This is test round "+i.ToString();
Console.WriteLine(s);
}
pt.Stop();
Console.WriteLine("Duration: {0} second(s)"n", pt.Duration); // print the duration of the timed code
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}
}
参考文献:
【1】http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx