dave317
dave317

Reputation: 774

How can I test execution time of a query in C# .NET core

Using Visual Studio 2017.

I have a line of code like this:

EndIndex = TotalPages <= PageButtons ? TotalPages : 
Math.Max(PageButtons, Math.Min((PageIndex + pageRange), TotalPages));

How can I can compare this to alternate ways of writing this same code for a result, like:

AltEndIndex = TotalPages <= PageButtons ? TotalPages : (PageButtons > (PageIndex + pageRange) ? PageButtons : (TotalPages < (PageIndex + pageRange) ? TotalPages
                : PageIndex + pageRange));

I am about to test that they do actually give the same result, but regardless, there are probably 5 different ways to come to the same result, I am trying to figure out what method is fastest.

Upvotes: 1

Views: 1057

Answers (2)

yaakov
yaakov

Reputation: 5850

BenchmarkDotNet is the golden standard for microbenchmarks such as this.

Upvotes: 3

user9622872
user9622872

Reputation:

You can use the Stopwatch class to create a timer to check ticks.

Stopwatch Code Reference and Examples

Compare one function with the other and use what's faster for you. :)

Upvotes: 0

Related Questions