Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created January 16, 2012 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamSaffron/1623514 to your computer and use it in GitHub Desktop.
Save SamSaffron/1623514 to your computer and use it in GitHub Desktop.
Dapper
// inserting 500 products.
// even when Dapper is running the same sql as EF it is twice as fast.
var products = Enumerable.Range(1, 500).Select(_ =>
new Product
{
Name = Guid.NewGuid().ToString(),
Description = Guid.NewGuid().ToString()
}
).ToList();
Stopwatch sw = Stopwatch.StartNew();
using (var ctx = new MyContext(cnn))
{
foreach (var p in products) ctx.Products.Add(p);
ctx.SaveChanges();
}
Console.WriteLine(sw.ElapsedMilliseconds);
// 2023
sw.Restart();
cnn.Open();
foreach (var p in products)
{
cnn.Query<int>(@"insert [dbo].[Products]([Name], [Description])
values (@Name, @Description)
select [Id]
from [dbo].[Products]
where @@ROWCOUNT > 0 and [Id] = scope_identity()", p);
}
Console.WriteLine(sw.ElapsedMilliseconds);
// 951
sw.Restart();
cnn.Execute("insert Products(Name,Description) values(@Name,@Description)", products.Select(p => new { p.Name, p.Description }));
Console.WriteLine(sw.ElapsedMilliseconds);
// 777
Console.ReadKey();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment