-3

I create a web service(web method) in c#.net 2008 and I want to evaluate the time that it takes to execute. I tried to use the StopWatch() function but it every time returns zero. Please give me a solution for this problem. Thanks alot.

user35443
  • 5,768
  • 11
  • 48
  • 69

3 Answers3

1

System.Environment.TickCount and the System.Diagnostics.Stopwatch class are two that work well for finer resolution and straightforward usage.

See Also:

Community
  • 1
  • 1
Moiz
  • 2,307
  • 5
  • 24
  • 47
0

At the start of your method do the following:

DateTime before = DateTime.Now;

At the end of the method block, you can get the execution time like so:

TimeSpan executionTime = DateTime.Now - before:
Caleb Keith
  • 804
  • 4
  • 9
  • What's wrong with `Stopwatch`? – Darin Dimitrov Dec 15 '12 at 17:53
  • He didn't post his code, its unnecessary to use a stopwatch anyways. – Caleb Keith Dec 15 '12 at 17:55
  • Why is it unnecessary? It's kind of designed for this task instead of working with datetimes, substractions, ... Also did you read this: http://stackoverflow.com/questions/28637/is-datetime-now-the-best-way-to-measure-a-functions-performance/28648? Simply put: don't use datetimes to measure performance. – Darin Dimitrov Dec 15 '12 at 18:49
  • It also states that it can increase execution time by using DateTime.UtcNow. – Caleb Keith Dec 16 '12 at 16:41
0

If you want to profile your application its best not to do it in code as this will give an inaccurate result. It takes time for your webservice to receive the request and start executing your code. Your best best would be to use a service like new relic to do an in depth profile of your running application

(NB. I'm not recommending new relic specifically, there are many other providers out there)

Tom Squires
  • 8,106
  • 9
  • 44
  • 68