Very easy way to time a process
I am currently working on a project where I need to transfer a certain file from a server to a client. My transfer is ok, but how ok is actually? I was asking myself: What is the delay if I send a certain file. How long does the process take to complete? Basically I wanted to stopwatch the time between the start and the end.
This isn’t really hard code, it’s just nice to know.
1: //to store the start time
2: private static DateTime StartTime;
3: //to store the end time
4: private static DateTime StopTime;
5:Â
6: //property to get the time between start and end
7: public static int TimeInMiliSeconds
8: {
9: get
10: {
11: TimeSpan ts = StopTime - StartTime;
12: //You could also get the Seconds, Minutes ..
13: return ts.TotalMilliseconds;
14: }
15: }
Â
To set and get the values you do the following
1: //Just before the process starts
2: StartTime = DateTime.Now;
3: ....
4: //When the process stops
5: StopTime = DateTime.Now;
6: //Display the time it took to process your process
7: Console.WriteLine("Time it took to process your process: "+ TimeInMiliSeconds +" miliseconds");
Â