我正在使用C#/ .NET 1.1;如何计算处理时间,例如将文件从1个系统复制到另一个系统?
答案 0 :(得分:15)
Stopwatch sw = new Stopwatch();
sw.Start();
CopyFile();
sw.Stop();
Console.WriteLine("Elapsed : {0}", sw.Elapsed)
此类在.NET 1.1中不可用,您可以使用QueryPerformanceCounter和QueryPerformanceFrequency 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)