Benchmarks : Comparing LINQ to NHibernate Transforms/Grouping
Yesterday, I wrote about setting up NHibernate to query up, group, count, and transform results and return them into a control. Why did I go to such effort? Well, the original LINQ query I had that refined the results didn’t perform up to par. As some may note, premature optimization is never a good practice so I needed some stats to back up the claims.
Overnight, I wrote up a quick test to query up both ways and benchmark the results. Here’s what I found.
public void TEMP_quick_compare_of_linq_to_nhibernate()
var schoolId = 120;
var benchmark = new Benchmark();
using (var repository = new IncidentRepository())
var resultsFromLinq =
foreach (var item in resultsFromLinq)
var resultsFromNhibernate =
foreach (var item in resultsFromNhibernate)
Setting up the benchmark (and the NHibernate Init) are outside of the benchmark—they’re necessary overhead. I’m also iterating through each of the results as part of the benchmark to ensure that everything is evaluated. Past that, pretty basic. On the database side, I’ve disabled statement caching to not sway the results as much.
With 24 records (the test data in the system), the results were pretty clear. The average of running the benchmark 100 times resulted in…
With 24 records, NHibernate was about 37x faster.
That’s nice, but what happens in a few weeks when there are a few thousand records? I populated a few hundred of each incident type into the system, giving me almost 4000 records (the anticipated monthly load of the system by the customer). How’d that change our averages?
Now we’re only 6x faster with NHibernate vs. LINQ. The duration from 24 to 4000 records for LINQ jumped ~.18 seconds for a 25% gain where as NHibernate jumped ~.11 seconds for a 626% gain.
So, with that, my original gut feeling and assumptions were wrong. More and more records don’t really slow down the LINQ filtering.. at least not by much. The performance gain is still appparent between the two methods (.88 sec vs. .13 sec); however, how much of that time is eaten up by rendering, server latency, etc and not by the actual processing?