How to measure execution time of a Action in C# and VB.NET

To measure execution time of a Action in C# and VB.NET you can use the following Extension Method snippet.

Sample C#

public static TimeSpan Time(this Action @this)
{
	var watch = Stopwatch.StartNew();
	@this();
	watch.Stop();
	return watch.Elapsed;
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function Time(this As Action) As TimeSpan
	Dim watch = Stopwatch.StartNew()
	this()
	watch.Stop()
	Return watch.Elapsed
End Function

Please Share us:
Please rate this snippet

One thought on “How to measure execution time of a Action in C# and VB.NET

Leave a Reply