Skip to content
February 14, 2008 / kiranpatils

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.

  1. 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());


}//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:

  1. Before execution starts take time using DateTime.Now
  2. After execution stops take time using DateTime.Now
  3. Check a difference using TimeSpan-which has so many useful properties.
  4. 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.


  1. I guess that you have Service ready in WSSF Solution and eager to test it with Client Side-WCSF.
  2. Now come to WCSF and add Reference of your service let’s say “Employee Proxy”.
  3. Employee Proxy has one function “BookingProxy.BookingResponse GetEmployeeByID(BookingProxy .bookingrequest)”
  4. Now when you call your service using BookingProxy in Controller before calling “GetEmployeeByID” service take a time as a start time
  5. and when service returns [next line of GetEmployeeByID] take this time as a Stop time
  6. Do a TimeSpan and you will have a time taken by your service.
  7. 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:



Leave a Comment
  1. saju / Apr 23 2008 2:16 pm


    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 ? )

  2. Pawel / Aug 18 2012 1:54 pm


  1. 2010 in review « A Place for C Sharpers/.Netters
  2. mahilaparty

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: