Entity Framework Core 2 Vs Dapper Performance Benchmark

Muhammad Mohsin
3 min readNov 29, 2018

--

Originally published and complete details on mmohsin.com.

Motivation:

Okay, i know that you have seen other benchmarks on the internet showing dapper is faster than Entity Framework. I found it unfair to compare an auto-generated SQL to the one written by a good developer, so decided to benchmark it for below use cases. Including linq vs Plain Queries comparisons and also the affect of AsNoTracking().

Use Cases:

  1. Select 5000 rows out of 500k rows.
  2. Insert new row.
  3. Update a row column at index 5001.

Source Code:

You can download the source code from git.

Test Environment:

  1. A SQL database server which is on LAN
  2. A table containing 500k rows and 50 columns.
  3. .Net Core 2 Console Application.

Nugets:

  1. Benchmark.NET
  2. Dapper
  3. Entity Framework Core 2

Dapper.Net Repository

Dapper’s query looks like this.

public class DapperRepository
{
public List GetUsersWithDapper()
{
using (IDbConnection db = new SqlConnection(@"YourConnectionString"))
{
return db.Query
($"select top 5000 * from users_general").ToList();
}
}
}

Entity Framework Core 2 Repository

public class EFRepository
{
public List GetUsersWithEF()
{
using (var db = new EFCore2TestContext())
{
return db.UsersGeneral.FromSql("Select top 5000 * from users_general").AsNoTracking().ToList();
//return db.UsersGeneral.Take(5000).AsNoTracking().ToList();
}
}
}

Notice that i used AsNoTracking() for performance hit. As it will indicate EF not to track the models for change.

Benchmark Class

public class EFCore2VsDapper
{
EFRepository _EFRepo;
DapperRepository _DapperRepo;
ADORepository _adoRepository;

public EFCore2VsDapper()
{
_EFRepo = new EFRepository();
_DapperRepo = new DapperRepository();
_adoRepository = new ADORepository();
}

[Benchmark]
public List GetUsersWithEntityFramework() => _EFRepo.GetUsersWithEF();

[Benchmark]
public List GetUsersWithDapper() => _DapperRepo.GetUsersWithDapper();

//[Benchmark] Uncomment if you want to add ADO.NET to the competition
//public List GetUsersWithADO() => _adoRepository.GetUsersWithADO();
}

Run It!

class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run(typeof(EFCore2VsDapper));

Console.WriteLine("Done");
Console.ReadLine();
}
}

Notice that Entity Framework Core 2’s plain sql commands are almost giving the same result as Dapper(very little difference). Also notice that AsNoTracking(), when applied gives faster response.

Caution:

Always run your code in release mode for performance benchmarks. Running a code in Debug mode can be 10x slower. So, publish it, got to the published directory and run the below command in cmd.

dotnet EFCore2VSDapper.dll

Benchmark.Net will execute your methods multiple times and will calculate average time taken.

Conclusion:

Dapper is faster than the Entity Frame Core 2. If performance is your biggest concern, Use dapper! However, if you want speedy development and can compromise a little bit on performance, use entity framework instead.

So, don’t select either based on my benchmark. You can download the source code and it is highly recommended to test it yourself for different scenarios before making a decision.

I’m missing something? or your tests gave different results? let me know by sharing your valuable feedback.

Looking to expand your technical team?👨👱 We at Code&Devs provide skilled dedicated developers. Let’s grow together.🏆

Thanks for reading. If you found this article useful, feel free to hit that clap button 👏 to help others find it.

Update (6/18/2019):
I am currently using both Entity Framework Core and Dapper in my current project. Entity Framework for insert, update, and get. And Dapper for complex queries like reports, joins (where the data is fetched from multiple tables). Entity Framework is reducing my development time and dapper is fetching complex data with more speed. Now i also have more control and flexibility and can decide where to use what. Now i believe that nothing works better than the combination of Entity Framework and Dapper!

--

--