Any tool that says how long each method takes to run?

萝らか妹 提交于 2019-12-21 01:11:30

问题


some parts of my program are way slow. and I was wondering if there is tool that i can use and for example it can tell me ok running methodA() took 100ms , etc ...or so useful info similar to that.


回答1:


The System.Diagnostics namespace offers a helpful class called Stopwatch, which can be used to time parts of your code (think of it as a "poor man's profiler").

This is how you would use it:

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); // Start timing

// This is what we want to time
DoSomethingWeSuspectIsSlow();

stopwatch.Stop();
Console.WriteLine("It took {0} ms.", stopwatch.ElapsedMilliseconds);



回答2:


If you are using Visual Studio Team System there is a builtin profiler in 'Performance Tools'. There is a ton of useful background on this at this blog.

I've found this extremely useful in identifying the 20% of my code that runs 80% of the time, and hence what I should worry about optimizing.

Another simple technique that can be surprisingly effective is to run your release code in the debugger, and interrupt it a few times (10 or so can be enough) while it's in the 'busy' state that you are trying to diagnose. You may find recurring callstack info that directs you to the general area of concern. Again, the 80/20 rule in effect.




回答3:


Those kind of applications are known as "profilers"

Here's one for example: example




回答4:


See our SD C# Profiler. It can provide function timings of the function by itself and/or all of its callees.



来源:https://stackoverflow.com/questions/3773473/any-tool-that-says-how-long-each-method-takes-to-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!