using System.Collections.Generic;
using System.Diagnostics;
using System.Data.Entity;
public static List<BenchmarkResult> BenchmarkResults = new List<BenchmarkResult>();
public static void Main()
var customers = GenerateCustomers(1000);
var clockSaveChanges = new Stopwatch();
var clockBulkInsert = new Stopwatch();
var clockBulkInsertNoOutput = new Stopwatch();
using (var context = new EntityContext())
context.Customers.AddRange(customers);
clockSaveChanges.Start();
BenchmarkResults.Add(new BenchmarkResult() { Action = "SaveChanges (EF)", Entities = customers.Count, Performance = clockSaveChanges.ElapsedMilliseconds + " ms" });
using (var context = new EntityContext())
context.BulkInsert(customers);
var timeFaster = Math.Round((double)clockSaveChanges.ElapsedMilliseconds / clockBulkInsert.ElapsedMilliseconds, 2);
var reducedPercent = Math.Round((double)(clockSaveChanges.ElapsedMilliseconds - clockBulkInsert.ElapsedMilliseconds) / clockSaveChanges.ElapsedMilliseconds, 2) * 100;
BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkInsert (Outputting values)", Entities = customers.Count, Performance = string.Format("{0} ms", clockBulkInsert.ElapsedMilliseconds), TimeFaster = string.Format("{0}x faster than SaveChanges", timeFaster), ReducedPercent = string.Format("Time reduced by {0}% compared to SaveChanges", reducedPercent) });
using (var context = new EntityContext())
clockBulkInsertNoOutput.Start();
context.BulkInsert(customers, options => options.AutoMapOutputDirection = false);
clockBulkInsertNoOutput.Stop();
var timeFaster = Math.Round((double)clockSaveChanges.ElapsedMilliseconds / clockBulkInsertNoOutput.ElapsedMilliseconds, 2);
var reducedPercent = Math.Round((double)(clockSaveChanges.ElapsedMilliseconds - clockBulkInsertNoOutput.ElapsedMilliseconds) / clockSaveChanges.ElapsedMilliseconds, 2) * 100;
BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkInsert (Not Outputting values)", Entities = customers.Count, Performance = string.Format("{0} ms", clockBulkInsertNoOutput.ElapsedMilliseconds), TimeFaster = string.Format("{0}x faster than SaveChanges", timeFaster), ReducedPercent = string.Format("Time reduced by {0}% compared to SaveChanges", reducedPercent) });
FiddleHelper.WriteTable(BenchmarkResults);
public static void JustInTime_Compile()
var customers = GenerateCustomers(20);
using (var context = new EntityContext())
context.Customers.AddRange(customers);
context.BulkDelete(customers);
using (var context = new EntityContext())
context.BulkInsert(customers, options => options.AutoMapOutputDirection = false);
context.BulkDelete(customers);
public static List<Customer> GenerateCustomers(int count)
var list = new List<Customer>();
for(int i = 0; i < count; i++)
list.Add(new Customer() { Name = "Customer_" + i, Description = "Description_" + i, IsActive = i % 2 == 0 });
public class EntityContext : DbContext
public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
public DbSet<Customer> Customers { get; set; }
public int CustomerID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Boolean IsActive { get; set; }
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }
public string Column7 { get; set; }
public string Column8 { get; set; }
public string Column9 { get; set; }
public class BenchmarkResult
public string Action { get; set; }
public int Entities { get; set; }
public string Performance { get; set; }
public string TimeFaster { get; set; }
public string ReducedPercent { get; set; }