preamble
NET development, in order to accurately count the execution time of the corresponding method, the most common way is to manually use Stopwatch to explicitly write the timing logic, but if you need to use a large number of Stopwatch for time-consumption statistics is not conducive to keeping the code clean and increase the cost of code maintenance.
Projects
MethodTimer is a .NET open source , free (MIT License), lightweight run-time statistics library , used to automatically inject timing code into the specified method at compile time , without having to manually write tedious timing logic .
Creating a Console Application
Creates a file named:MethodTimerExercise
of the console application.
Installing NuGet
command installation
PM> Install-Package Fody
PM> Install-Package
NuGet Package Manager Installation
look for sth.Fody
Installation:
look for sth.Installation:
Quick access
By adding the methodTime
attribute, MethodTimer will automatically send a compilation-time message to theTimeMethod
Inject timing code.
[Time]
public static void TimeMethod()
{
for (int i = 0; i < 100; i++)
{
($"Output result {i}");;
}
}
Viewing compiled code with ILSpy
public static void TimeMethod()
{
Stopwatch stopwatch = ();
try
{
for (int i = 0; i < 100; i++)
{
($"Output result {i}");;
}
}
finally
{
();
string message = null;
(((RuntimeMethodHandle)/*OpCode not supported: LdMemberToken*/, typeof(Program).TypeHandle), , message);
}
}
Time-consuming interceptors record two methods
Runs in long (milliseconds):
/// <summary>
/// Running time is long (milliseconds)
/// </summary>
public static class MethodTimeLogger1
{
public static void Log(MethodBase methodBase, long milliseconds, string message)
{
($"Method: {} Time taken: {milliseconds} milliseconds, message: {message}");;
}
}
The running time consumed is TimeSpan:
/// <summary>
/// The runtime is TimeSpan.
/// </summary>
public static class MethodTimeLogger
{
public static void Log(MethodBase methodBase, TimeSpan elapsed, string message)
{
($"Method: {} Time Consumed: {} milliseconds, Message: {message}");;
}
}
Time-consuming statistical hourly output
Project source code address
More useful features and characteristics of the project welcome to the project open source address to view 👀, do not forget to give the project a Star support 💖.
- Open source address:/Fody/MethodTimer
- MethodTimerExercise:/YSGStudyHards/DotNetExercises/tree/master/MethodTimerExercise
A selection of great projects and frameworks
This project has been included in the C#/.NET/.NET Core Excellent Projects and Frameworks Selection, focusing on the excellent projects and frameworks selection can let you keep abreast of the latest developments and best practices in the field of C#, .NET and .NET Core, and improve the efficiency and quality of development work. The pit has been dug, you are welcome to submit PR recommendations or self-recommendation (so that excellent projects and frameworks are not buried 🤞).
- GitHub open source address:/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/
- Gitee open source address:/ysgdaydayup/DotNetGuide/blob/main/docs/DotNet/