计算处理时间

max*_*axy 2 c#

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

Tho*_*que 15

System.Diagnostics.Stopwatch

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

此类在.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)
Run Code Online (Sandbox Code Playgroud)