I want to calculate the time taken by the system for performing a particular operation in asp.net using C#. When a user clicks on a button to get any result, i have to show the result as well as the time taken for them to get the result. Anyone please tell the code which i have to give to display the time taken.
-
2Do you mean time taken on the server side (i.e. within the ASP.Net code)? Or do you mean time as experienced by the user? The latter will be highly dependent on network latency etc. rather than the ASP.Net# back-end code. – StevieB Jan 23 '14 at 22:31
-
I need to calculate the time taken on the server side. – user3229818 Jan 24 '14 at 01:31
2 Answers
In case you want to measure how much time a certain operation takes then you can use a StopWatch object. Example on the usage below:
class Program
{
static void Main()
{
// Create new stopwatch
Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();
// Do something
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(1);
}
// Stop timing
stopwatch.Stop();
// Write result
Console.WriteLine("Time elapsed: {0}",
stopwatch.Elapsed);
}
}
So what you need to do in your scenario is to start a StopWatch when starting the execution of your Action/Method and stop it before returning a view/ finishing the execution. You can then use the stopWatch.Elapsed value to display it to the user.

- 6,176
- 21
- 26
-
1+1. Note that basic usage of `Stopwatch` as shown here will only measure how long actual operation took, ignoring all infrastructure code on server side as well all network time/JavaScript time - so will always be less than user observe. – Alexei Levenkov Jan 23 '14 at 22:37
-
The original poster specified ASP.Net. The easiest way to do this in ASP.Net is probably to inject something like this into your global.asax
:
private static ILog log = log4net.LogManager.GetLogger( typeof(Global) ) ;
void Application_BeginRequest( object sender , EventArgs e )
{
Stopwatch timer = new Stopwatch();
timer.Start();
Request.RequestContext.HttpContext.Items["timer"] = timer ;
return ;
}
void Application_EndRequest( object sender , EventArgs e )
{
Stopwatch timer = (Stopwatch) this.Request.RequestContext.HttpContext.Items["timer"] ;
timer.Stop() ;
log.InfoFormat( "HttpVerb={0}, URL={1}, elapsed time={2}" ,
this.Request.RequestType ,
this.Request.Url ,
timer
) ;
return ;
}
If you want to do this on a page-specific basis, you'll need to do something similar, hooking the Page.PreInit and Page.Unload events respectively.
And you pretty much need to use the HttpContext
to keep the timer about for the duration of the request: if you try to use a static variable, you'll have your classic race condition.

- 71,308
- 16
- 93
- 135
-
The code provided in this answer is not guaranteed to work correctly. A Stopwatch instance must be stopped by the exact same thread that started it, and the ASP.NET runtime might hop threads between your BeginRequest and your EndRequest methods. This could result in incorrect values being logged. – Levi Jan 24 '14 at 17:11
-
Normally, an ASP.Net request is handled by a single worker thread. The only context in which the thread might be changed is if you are using asynchronous operations, which can cause the request's worker thread to switched. (see http://www.lhotka.net/WeBlog/PermaLink,guid,019e3c37-38ed-492e-b769-16e1a57fed0a.aspx and http://piers7.blogspot.fr/2005/11/threadstatic-callcontext-and_02.html and http://forum.springframework.net/showthread.php?572-CallContext-vs-ThreadStatic-vs-HttpContext&highlight=LogicalThreadContext). – Nicholas Carey Jan 24 '14 at 18:28
-
ASP.NET request processing can (and often does!) hop threads. Remember: both IIS and ASP.NET have code running between your application's BeginRequest and EndRequest events, and this code is not guaranteed to execute synchronously. A thread hop can occur due to IIS or ASP.NET's code in these events. See http://stackoverflow.com/questions/4791208/threadstaticattribute-in-asp-net for more information. – Levi Jan 25 '14 at 22:15