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.
Asked
Active
Viewed 1,543 times
-3
![](../../users/profiles/948909.webp)
user35443
- 5,768
- 11
- 48
- 69
![](../../users/profiles/1906640.webp)
lovely baby
- 1
- 1
-
3can you give us some code sample? – Alex Filipovici Dec 15 '12 at 17:49
-
6Hi lovely baby, welcome to stackoverflow, please read [faq] and [ask] – Soner Gönül Dec 15 '12 at 17:50
-
1[What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Darin Dimitrov Dec 15 '12 at 17:52
-
5@SonerGönül your comment sounded _very_ weird until I read the author's name – emartel Dec 15 '12 at 17:54
-
source code would be nice to share, otherwise we can't help you – cristi _b Dec 15 '12 at 18:17
3 Answers
1
System.Environment.TickCount and the System.Diagnostics.Stopwatch class are two that work well for finer resolution and straightforward usage.
See Also:
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:
![](../../users/profiles/1898430.webp)
Caleb Keith
- 804
- 4
- 9
-
-
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)
![](../../users/profiles/648814.webp)
Tom Squires
- 8,106
- 9
- 44
- 68