【问题标题】:Performance Check for C# Method CallsC# 方法调用的性能检查
【发布时间】:2014-07-18 06:09:23
【问题描述】:

我想创建一个类,它编写一个文件,其中包含层次结构中的所有方法调用以及我的程序所需的时间。我不知道如何解决这个问题。我发现的只是 StackTrace 和 StackFrame 类,但这并不是我真正需要的。我想启动我的程序并执行一些操作。在后台,我的班级编写了一个具有以下或类似结构的文件(例如 XML):

Method1: 220ms
  Method2: 180ms
    Method3: 150ms
  Method4: 25ms
    Method5: 20ms
Method6: 110ms

有没有简单的方法来做到这一点?我实际上是在 System.Diagnostics 和 System.Reflection 命名空间中搜索,但我不知道该怎么做。

到目前为止,我还没有写过一行代码,因为我需要一个好的提示。

提前致谢。

【问题讨论】:

标签: c# reflection system.diagnostics


【解决方案1】:

你可以试试Mini Profiler。我通常用它来跟踪执行时间。

然后跟踪方法执行此操作(如文档中提及的那样)-

using StackExchange.Profiling;
...
var profiler = MiniProfiler.Current; // it's ok if this is null
using (profiler.Step("Set page title"))
{
    ViewBag.Title = "Home Page";
}
using (profiler.Step("Doing complex stuff"))
{
    using (profiler.Step("Step A"))
    { // something more interesting here
        Thread.Sleep(100);
    }
    using (profiler.Step("Step B"))
    { // and here
        Thread.Sleep(250);
    }
}

它有一个漂亮的界面,可以向您显示网络上每个步骤需要多少时间 -

对于基于 Windows 和控制台的应用程序,请使用此处提到的 Windows 扩展 -

http://nootn.github.io/MiniProfiler.Windows/

这里提到了这个过程-

http://www.nootn.com.au/2012/07/performance-profiling-console-or-winwpf.html#.U8i-TDS1bcg

【讨论】:

  • 您的帖子感觉像是评论...确保添加实际答案如何自己实施。
猜你喜欢
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
相关资源
最近更新 更多