连接 .NET 方法以进行分析
可能的重复:
分析方法C# 知道运行需要多长时间
我需要检查运行一个方法需要多长时间。
例如,我需要知道在我的项目中 using System.Reflection
中运行 GetTypes()
方法需要多长时间。
foreach (Type t2 in a.GetTypes())
{
Console.WriteLine(t2);
}
最简单的方法可能是运行分析器或插入 Stopwatch
所有使用 GetTypes() 的源文件,但我希望我可以使用挂钩 GetTypes() 方法在运行之前和之后启动和退出秒表GetTypes() 方法。
sw = Stopwatch.StartNew();
foreach (Type t2 in a.GetTypes())
{
Console.WriteLine(t2);
}
sw.Stop();
我可以使用 C#/.NET(Mono) 做到这一点吗?如果没有,除了修改源代码或运行探查器之外,我还有其他选择吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以您的示例为例:
您测量的不仅仅是 GetTypes,您还测量 Console.WriteLine 以及其他所有内容。
编写一个钩子(如 Moles)可能会对性能产生太大影响(但是我必须承认我以前从未做过类似的事情,但我确实注意到运行具有 Moles 的单元测试时性能的差异)。
最简单的选择(也是最便宜的)是下载以下(免费)分析器之一:
AQTime(免费版本)http://smartbear.com/products/free-tools/aqtime-standard/
SharpDevelop(有分析器):http://www.icsharpcode.net/opensource/sd/download/
XTE Profiler http://www.xteprofiler.com
其中任何一个都会为您提供详细信息。但如果您真的想自己动手,请下载 SharpDevelop 的源代码并尝试弄清楚他们是如何制作分析器的。
With your example:
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.