maxy
maxy

Reputation: 438

Calculate processing time

I am using C# / .NET 1.1; how can I calculate the processing time, for example for copying a file from 1 system to another?

Upvotes: 2

Views: 8508

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292345

System.Diagnostics.Stopwatch

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

This class in not available in .NET 1.1, instead you can use the QueryPerformanceCounter and 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)

Upvotes: 14

Related Questions