Checking Execution Time with C#.NET+ Use of it In WSSF+WCSF
Today I am going to show you how to check an Execution time-Time taken by a function for executing its code block.
In today’s fast world all wants fast task [1Mbps, 1 Gbps…] and if your application’s response time is not fast than Boss you are lost with your application.
As per my Mind I ask “Why?” Why I need to check execution time because client wants final product for that I have to write code than why should I do this Execution time stuff.
For answering above question. Let us see some examples
Example1. Suppose you logged in your Banking site in which you have link called “View last transactions” and suppose that you are a shop keeper and have a current account than it’s obvious that you will have so many daily transactions. Now you clicked on link and from last 10 mins. Page says “Please wait…..” I am sure that you will close the browser window and will go physically at bank’s branch and ask for bank statement. [Gradually people won’t believe in software, applications means our Job is in danger.]
Example2. Now suppose you have U.S.A. Client for whom you are creating one software. And if response time is too slow than he will say “Indian peoples can’t create good software”. And will suggest to others also that don’t ask Indian people to develop your software. [Everything will be ost!!!!!!].
Now you are agree with me that response time is too important in our field.
So, let’s have a look on it how to do it with C#.NET with one example.
- I have created one Console Application for testing it. Its code is shown as below.
DateTime ExecutionStartTime; //Var will hold Execution Starting Time
DateTime ExecutionStopTime;//Var will hold Execution Stopped Time
TimeSpan ExecutionTime;//Var will count Total Execution Time-Our Main Hero
ExecutionStartTime = DateTime.Now; //Gets the system Current date time expressed as local time
//this is the main block for which we are checking execution Time
for (int i = 0; i < count; i++)
{ //Code of Block to do Execution
Console.WriteLine(“Hi i am”+i.ToString());
Console.Clear();
}//Execution Completed
ExecutionStopTime = DateTime.Now; //Gets the system Current date time expressed as local time
//Now Just calculate the duration taken by a Block
ExecutionTime = ExecutionStopTime – ExecutionStartTime; //Total Time
Console.WriteLine(“********Execution Time Summary*********”);
Console.WriteLine(“Loop Count = “+count.ToString());
Console.WriteLine(“TotalHours = “ + ExecutionTime.TotalHours.ToString());//Total Hours Console.WriteLine(“TotalMinutes = “ + ExecutionTime.TotalMinutes.ToString());//TotalMinutes Console.WriteLine(“TotalSeconds = “ + ExecutionTime.TotalSeconds.ToString());//TotalSeconds Console.WriteLine(“TotalMilliseconds = “ + ExecutionTime.TotalMilliseconds.ToString());//TotalMilliseconds Console.WriteLine(“***************************************”);
OutPut when Input = 10000
Means my function has taken 2.03125 Second and 2031.25 Milliseconds.
So now just check the function do some “Code optimization” and check the Execution time. Decrease it as possible as you can.
Quick Check:
- Before execution starts take time using DateTime.Now
- After execution stops take time using DateTime.Now
- Check a difference using TimeSpan-which has so many useful properties.
- That’s it.
Real world use in WSSF+WCSF:
Microsoft has developed software factories and repository factories and they say that it has too quick response time. But than how you will check it that your service has been written good enough to respond quickly.
You can use the above steps for checking service response time also. Let me show with one example how you can do it.
Example:
- I guess that you have Service ready in WSSF Solution and eager to test it with Client Side-WCSF.
- Now come to WCSF and add Reference of your service let’s say “Employee Proxy”.
- Employee Proxy has one function “BookingProxy.BookingResponse GetEmployeeByID(BookingProxy .bookingrequest)”
- Now when you call your service using BookingProxy in Controller before calling “GetEmployeeByID” service take a time as a start time
- and when service returns [next line of GetEmployeeByID] take this time as a Stop time
- Do a TimeSpan and you will have a time taken by your service.
- So now you will have a code which looks like as under:
DateTime ExecutionStartTime = DateTime.Now;
BookingProxy.BookingResponse response = GetEmployeeByID (BookingProxy .bookingrequest);
DateTime ExecutionStopTime = DateTime.Now;
TimeSpan ExecutionTime = ExecutionStopTime – ExecutionStartTime;
Note it down and Enjoy!!!
Happy Executing optimum code block!!!!
-Kiran Patil
Reference Link:http://www.codersource.net/csharp_measure_execution_time.aspx
HI,
I am trying to measure the time using DateTime.Now.Ticks … the difference sometimes turns out to be zero.
( is the timer ticks not updated properly ? )
Look at this one using Stopwatch class.
http://blogs.msdn.com/b/shawnhar/archive/2009/07/07/profiling-with-stopwatch.aspx