Last active
January 12, 2017 03:02
-
-
Save JonathanMagnan/4680cb84bdd9cf10bc23db036709406e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Benchmark() | |
{ | |
// BENCHMARK using Stopwatch | |
var clock1 = new Stopwatch(); | |
var clock2 = new Stopwatch(); | |
var nbRecord = 1000; | |
var nbTry = 5; | |
var list = GenerateData(nbRecord); | |
// BENCHMARK: JIT compile library first | |
Test1(list, null); | |
Test2(list, null); | |
for (var i = 0; i < nbTry; i++) | |
{ | |
Test1(list, clock1); | |
Test2(list, clock2); | |
} | |
var r1 = clock1.ElapsedMilliseconds/nbTry; | |
var r2 = clock2.ElapsedMilliseconds/nbTry; | |
} | |
public void Test1(List<string> lines, Stopwatch clock) | |
{ | |
using (var ctx = new CustomerContext()) | |
{ | |
var customers = new List<Customer>(); | |
foreach (var line in lines) | |
{ | |
var customer = new Customer(); | |
// ...code... | |
customers.Add(customer); | |
} | |
ctx.Customers.AddRange(customers); | |
// BENCHMARK: Only method we want to test | |
clock.Start(); | |
ctx.BulkSaveChanges(); | |
clock.Stop(); | |
} | |
} | |
public void Test2(List<string> lines, Stopwatch clock) | |
{ | |
using (var ctx = new CustomerContext()) | |
{ | |
var customers = new List<Customer>(); | |
foreach (var line in lines) | |
{ | |
var customer = new Customer(); | |
// ...code... | |
customers.Add(customer); | |
} | |
ctx.Customers.AddRange(customers); | |
// BENCHMARK: Only method we want to test | |
clock.Start(); | |
ctx.SaveChanges(); | |
clock.Stop(); | |
} | |
} | |
public List<string> GenerateData(int nbRecord) | |
{ | |
var list = new List<string>(); | |
for (var i = 0; i < nbRecord; i++) | |
{ | |
list.Add(i.ToString()); | |
} | |
return list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment