I'm trying to measure the execution time of processes within C#. This is the code I'm using to do this:
private static double TimeProcess(string name, string workingDirectory, string arguments)
{
Process process = new Process();
process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.FileName = name;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = arguments;
process.Start();
process.WaitForExit();
return process.TotalProcessorTime.TotalMilliseconds;
}
I am aware that the usual way of benchmarking is to instantiate a stopwatch and use that to measure the elapsed time, however, I am wondering if a stopwatch would be appropriate in this case, since it would be subject to the influence of scheduling for example. My approach would guarantee to measure only the time that the process spends in execution.
My two questions:
- Am I wrong in assuming that a stopwatch would be less accurate/an inferior choice in this case?
- How precise is Process.TotalProcessorTime? I can see that it calls the kernel32 method "GetProcessTimes", but I am having a hard time finding information about how this method actually works. Meanwhile, there is plenty information available about the "QueryPerformanceCounter" method that the stopwatch uses.