Entity Framework 6 vs. NHibernate smackdown!

It’s time for a performance test between EF-6 and NHibernate.  Previously I did a smackdown between EF-6 and LINQ-to-SQL (see this article: Entity Framework 6 vs. LINQ-toSQL smackdown!).  This time I’m going to show the performance of inserts between EF-6 and NHibernate.

I used the same tables and data as in the previous blog posting. The code I used for NHibernate to insert records looks like this (I also created code for select, update and delete):

using (db.BeginTransaction())
{
    for (int j = 0; j < 10; j++)
    {
        for (int i = 0; i < 1000; i++)
        {
            Person personRecord = new Person()
            {
                first = firstnames[i],
                last = lastnames[i],
                department = 1
            };

            db.Save(personRecord);
        }
    }
    db.Transaction.Commit();
}

Here are the results from the CRUD tests comparing EF-6 with NHibernate (I also threw in the latest test run of LINQ-to-SQL for comparison):

OK, now for a few caveats:

  1. I ran each test 5 times and chose the fastest time.
  2. LINQ-to-SQL is not optimized for maximum speed.
  3. These times are based on my machine at the time that I ran these programs.
  4. These tests are only for general cases.
  5. I put the select query in a loop to run 1000 times to obtain a measurement.

Generally speaking it appears that NHibernate is quite a bit faster than either EF-6 or LINQ-to-SQL for CRUD operations. The select query is not a very good test. In fact, the bulk of the time could be attributed to the looping code itself since my guess is that a select is all done within the ORM once the data has been cached. A more effective test would involve 5 to 10 large tables of data using multiple types of select cases (like subqueries and outer joins, order by’s and group by’s). Another test that should be performed as a baseline, would be a direct sql query without the aid of an ORM. This should be faster than all of these ORMs.

To obtain the code, download this zip file containing all 3 test projects:

ORMSpeedTestProjects.zip

If you download and use these projects, don’t forget to change the configuration settings for each project to point to your database!

Leave a Reply