Skip to content

Instantly share code, notes, and snippets.

@bertwagner
Created February 20, 2017 00:45
Show Gist options
  • Save bertwagner/2acd19d8bfea89604cc53f9d6a46b4ca to your computer and use it in GitHub Desktop.
Save bertwagner/2acd19d8bfea89604cc53f9d6a46b4ca to your computer and use it in GitHub Desktop.
Comparing performance between SQL Server 2016 and Json.Net JSON parsing
static void Main(string[] args)
{
string cars = @"[ {""year"":2001,""make"":""ACURA"",""model"":""CL""}, ... ]";
Stopwatch stopwatch = new Stopwatch();
// Test #1
stopwatch.Start();
var deserializedCars = JsonConvert.DeserializeObject<IEnumerable<Car>>(cars);
stopwatch.Stop();
long elapsedMillisecondsDeserialize = stopwatch.ElapsedMilliseconds;
// Test #2 & #3
stopwatch.Restart();
var queriedCars = JsonConvert.DeserializeObject<IEnumerable<Car>>(cars).Where(x=>x.model == "Golf");
stopwatch.Stop();
long elapsedMillisecondsQuery = stopwatch.ElapsedMilliseconds;
// Test #4
stopwatch.Restart();
var serializedCars = JsonConvert.SerializeObject(deserializedCars);
stopwatch.Stop();
long elapsedMillisecondsSerialize = stopwatch.ElapsedMilliseconds;
// smaller data
string carsSmall = @"[ {""year"":2001,""make"":""ACURA"",""model"":""CL""}, {""year"":2001,""make"":""ACURA"",""model"":""EL""}, {""year"":2001,""make"":""ACURA"",""model"":""INTEGRA""}, {""year"":2001,""make"":""ACURA"",""model"":""MDX""}, {""year"":2001,""make"":""ACURA"",""model"":""NSX""}, {""year"":2001,""make"":""ACURA"",""model"":""RL""}, {""year"":2001,""make"":""ACURA"",""model"":""TL""}]";
// Test #5
stopwatch.Restart();
var deserializedCarsSmall = JsonConvert.DeserializeObject<IEnumerable<Car>>(carsSmall);
stopwatch.Stop();
long elapsedMillisecondsDeserializeSmall = stopwatch.ElapsedMilliseconds;
// Test #6
stopwatch.Restart();
var serializedCarsSmall = JsonConvert.SerializeObject(deserializedCarsSmall);
stopwatch.Stop();
long elapsedMillisecondsSerializeSmall = stopwatch.ElapsedMilliseconds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment