gpt4 book ai didi

c# - C# 方法调用的性能检查

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:40 25 4
gpt4 key购买 nike

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

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

有没有简单的方法来做到这一点?我实际上在 System.Diagnostics 和 System.Reflection 命名空间中搜索,但我不知道如何执行此操作。

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

提前致谢。

最佳答案

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

然后跟踪方法执行此操作(如文档中的 mentioend)-

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);
}
}

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

enter image description here

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

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

这里提到了这个过程-

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

关于c# - C# 方法调用的性能检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24818271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com