Reputation: 59
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:
Upvotes: 6
Views: 367