Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time process takes to complete in seconds?

My program runs a batch file in cmd.exe, after it finished I want to display a MessageBox to user saying Finished in #.## seconds,

I'm redirecting CMD output to a textbox using process.BeginOutputReadLine(), this is the code I tried:

if (e.Data == null)
{  
    string time = process.TotalProcessorTime.Seconds.ToString();
    MessageBox.Show("Finished in " + time + " seconds");
}

It took about 7-15 seconds to complete the process, but the MessageBox displayed Finished in 0 seconds.

How do I get the accurate time it took to complete in seconds?

like image 618
Abraham Avatar asked May 16 '13 19:05

Abraham


People also ask

How do you calculate Execution time?

The difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.

What does %% time mean in Python?

%%time is a magic command. It's a part of IPython. %%time prints the wall time for the entire cell whereas %time gives you the time for first line only. Using %%time or %time prints 2 values: CPU Times.

How do you calculate time taken to run a method in Python?

In order to calculate the time elapsed in executing a code, the time module can be used. Save the timestamp at the beginning of the code start using time() . Save the timestamp at the end of the code end . Find the difference between the end and start, which gives the execution time.


2 Answers

Stopwatch watch = new Stopwatch();
watch.Start();
//Do things
watch.Stop();
Text = watch.Elapsed.TotalSeconds.ToString();
like image 159
ispiro Avatar answered Oct 19 '22 23:10

ispiro


Have you tried process.ExitTime.Subtract(process.StartTime).TotalSeconds?

Edited to add: Note that you will get an exception if you try to use this before the process has exited. Per the documentation for ExitTime, use the HasExited property if you have any doubt as to whether this is the case or not.

like image 38
Dominic P Avatar answered Oct 19 '22 23:10

Dominic P