mnsr
mnsr

Reputation: 12437

LINQ - Get total query results time

I would like to know if it is possible to get a query 'search results' time? Like how google has a time under each query - this is the time it took to complete the search.

How is this achievable? I haven't attempted anything (hence no code) because, well, I'm not sure where to start.

LINQPad has this feature built in, but I'd like to get something similar going on an internal mvc app I'm working on.

Thanks

Upvotes: 5

Views: 3245

Answers (2)

TheEvilPenguin
TheEvilPenguin

Reputation: 5672

You can use a System.Diagnostics.Stopwatch object, start it when the page starts rendering and stop it at the end.

Upvotes: 0

Douglas
Douglas

Reputation: 54877

You could use the Stopwatch class to measure the duration of the execution of your query (or any other code).

Stopwatch sw = Stopwatch.StartNew();
var result = query.ToList();
TimeSpan elapsed = sw.Elapsed;
Console.WriteLine("Query took: " + elapsed);

In the case of LINQ queries, don’t forget that these are only evaluated on demand using deferred execution. Thus, you would probably want to force their enumeration (using, for example, ToList) in order to get a true measure of their execution’s duration.

Upvotes: 15

Related Questions