Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ - Get total query results time

Tags:

c#

linq

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

like image 327
mnsr Avatar asked Jun 04 '12 05:06

mnsr


1 Answers

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.

like image 94
Douglas Avatar answered Oct 02 '22 15:10

Douglas