我有一个使用秒表实例的功能,通过启动和停止来测量方法的时间。 我可以以某种方式在属性中定义该函数,只是用该属性修饰任何给定的方法来测量该方法的时间吗? 这会减少LOC。 我不想使用任何第三方库。
public class AppTrace:BaseModel<AppTrace>
{
[DataMember]
public string Comment;
[DataMember]
public string MethodName;
[DataMember]
public DateTime StartTimeStamp;
[DataMember]
public int Duration;
[DataMember]
public int UserObjectId;
[DataMember]
public string MachineName;
[DataMember]
public int ToolId;
private System.Diagnostics.Stopwatch stpWatch;
public AppTrace(string comment,string methodName,int userObjectId ,string machineName = "",int? toolId=null)
{
MethodName = methodName;
UserObjectId = userObjectId;
StartTimeStamp = DateTime.UtcNow;
Comment = comment;
MachineName = machineName;
stpWatch = new System.Diagnostics.Stopwatch();
stpWatch.Start();
}
public AppTrace()
{
}
public void CloseTrace()
{
this.stpWatch.Stop();
Duration=Convert.ToInt32( this.stpWatch.ElapsedMilliseconds);
}
}
如果不属性,我可以在代表的帮助下完成吗?
答案 0 :(得分:0)
正如我在评论中所说,一般情况下不可能。您可以使用一些AOP框架,它可以处理这些事情而非常成功。或者写一些调用实用程序。
在最简单的情况下,您的实用程序可能如下所示:
public static class Invoke
{
public static TimeSpan Measure(Action action)
{
if (action == null)
{
throw new ArgumentNullException();
}
var stopWatch = Stopwatch.StartNew();
action();
stopWatch.Stop();
return stopWatch.Elapsed;
}
}
然后您将能够测量方法调用时间。例如,类Foo
将用作度量目标。
// don't use such naming in production code
public class Foo
{
public void DoWork1()
{
// imitating hard computations
Thread.Sleep(TimeSpan.FromSeconds(5));
}
public void DoWork2(int param1)
{
// imitating hard computations
Thread.Sleep(param1);
}
}
public static class Program
{
public static void Main()
{
var foo = new Foo();
// signature of foo.DoWork1 is suitable for Invoke.Measure,
//so you can use it as MethodGroup
var time1 = Invoke.Measure(foo.DoWork1);
Console.WriteLine("Time for 'DoWork1' is {0}s", time1.TotalSeconds);
// signature of foo.DoWork2 is not suitable for Invoke.Measure,
// so you have to call it inside additional lambda
var time2 = Invoke.Measure(() => foo.DoWork2(42));
Console.WriteLine("Time for 'DoWork2' is {0}s", time2.TotalSeconds);
}
}
对于返回某些值但仍可以实现的方法将更难。添加辅助类Timed<T>
,它将包含返回值和操作持续时间。
public class Timed<T>
{
public readonly TimeSpan Duration;
public readonly T Result;
// making constructor private to force static factories usage
private Timed(T result, TimeSpan duration)
{
Result = result;
Duration = duration;
}
// static factory method instead of constructor, for generic parameter
// type inference
public static Timed<T> Create(T result, TimeSpan duration)
{
return new Timed<T>(result, duration);
}
}
然后需要Invoke.Measure
的重载。
public static class Invoke
{
// Func<T> instead of Action
public static Timed<T> Measure(Func<T> func)
{
if (func== null)
{
throw new ArgumentNullException();
}
var stopWatch = Stopwatch.StartNew();
var result = func();
stopWatch.Stop();
return Timed.Create(result, stopWatch.Elapsed);
}
}
顺便说一句F# REPL
有#time
指令来衡量调用时间。您可以从.NET
拨打任何F#
代码。它已集成到Visual Studio中,或者可以从命令行(fsi.exe
)调用。我不知道你的实际目标,所以它可能是合适的。