gpt4 book ai didi

C# 2.0 执行时间计时器

转载 作者:太空狗 更新时间:2023-10-29 23:04:28 25 4
gpt4 key购买 nike

我希望能够计算出执行多个函数所花费的时间。我正在考虑使用某种类型的秒表类。我可以在每次函数调用之前和之后调用启动/停止,但这对我来说太丑陋了。有没有秒表类的方法可以做到这一点? Reflections 类中的某些内容可以帮助解决这个问题吗?

预先感谢您的输入。

最佳答案

我认为你必须尝试 PostSharp 方式 http://www.postsharp.org/

为此更改示例中的代码:

public class ProfileMethodsAttribute : OnMethodBoundaryAspect 
{
public override void OnEntry( MethodExecutionEventArgs eventArgs)
{ eventArgs.MethodExecutionTag = Stopwatch.StartNew(); }

public override void OnExit( MethodExecutionEventArgs eventArgs)
{
// Here u can get the time
Console.WriteLine(((Stopwatch)eventArgs.MethodExecutionTag).ElapsedMilliseconds);
}
}

你可以通过任何方式请假

关于C# 2.0 执行时间计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2013804/

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