计算处理时间

时间:2009-09-24 09:36:13

标签: c#

我正在使用C#/ .NET 1.1;如何计算处理时间,例如将文件从1个系统复制到另一个系统?

1 个答案:

答案 0 :(得分:15)

System.Diagnostics.Stopwatch

Stopwatch sw = new Stopwatch();
sw.Start();
CopyFile();
sw.Stop();
Console.WriteLine("Elapsed : {0}", sw.Elapsed)

此类在.NET 1.1中不可用,您可以使用QueryPerformanceCounterQueryPerformanceFrequency API

[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool QueryPerformanceFrequency(out long lpFrequency);

...

long start;
long end;
long freq;
QueryPerformanceCounter(out start);
CopyFile();
QueryPerformanceCounter(out end);
QueryPerformanceFrequency(out freq);
double seconds = (double)(end - start) / freq;
Console.WriteLine("Elapsed : {0} seconds", seconds)