Hooking up .NET method for profiling purposes [duplicate]

余生颓废 提交于 2020-01-05 03:38:25

问题


Possible Duplicate:
Profiling a method in C# to know how long does it take to run

I need to check how long does it take to run a method.

For example, I need to know how long does it take to run GetTypes() method in using System.Reflection in my project.

foreach (Type t2 in a.GetTypes())
{
    Console.WriteLine(t2);
}

The easiest way might be running profiler or insert Stopwatch all the source files that uses GetTypes(), but I hope I could use hooking the GetTypes() method to start and quit the stop watch before and after running GetTypes() method.

sw = Stopwatch.StartNew();
foreach (Type t2 in a.GetTypes())
{
    Console.WriteLine(t2);
}
sw.Stop();

Can I do that with C#/.NET(Mono)? If not, do I have other options than modifying the source code or running the profiler?


回答1:


With your example:

sw = Stopwatch.StartNew();
foreach (Type t2 in a.GetTypes())
{
    Console.WriteLine(t2);
}
sw.Stop();

you are measuring much more than GetTypes, you are measuring Console.WriteLine and everything else as well.

Writing a hook (like Moles) would probably influence the performance too much (however I must admit that I have never done something like that before, but I do notice the difference in performance when running unit tests that have Moles).

The easiest option (and cheapest) is to download one of these (free) profilers:

AQTime (free version) http://smartbear.com/products/free-tools/aqtime-standard/

SharpDevelop (has a profiler): http://www.icsharpcode.net/opensource/sd/download/

XTE Profiler http://www.xteprofiler.com

Any of these will give you detailed information. But if you really want to do it yourself, download the source of SharpDevelop and try to figure out how they made the profiler.



来源:https://stackoverflow.com/questions/6244549/hooking-up-net-method-for-profiling-purposes

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