public void NewtonsoftStream() { Stopwatch timer = Stopwatch.StartNew(); long parseTime = 0; long convertTime = 0; for (int i = 0; i < 4; i++) { using (StreamReader reader = new StreamReader(DeserializePerfTests.LargeFileName, detectEncodingFromByteOrderMarks: true)) using (JsonReader jsonReader = new JsonTextReader(reader)) { Stopwatch innerTimer = Stopwatch.StartNew(); JObject value = JObject.Load(jsonReader); parseTime += innerTimer.ElapsedMilliseconds; innerTimer.Restart(); BabyNames names = value.ToObject <BabyNames>(); convertTime += innerTimer.ElapsedMilliseconds; GC.KeepAlive(value); GC.KeepAlive(names); } } long totalTime = timer.ElapsedMilliseconds; this.output.WriteLine($"Total time: {totalTime / 1000.0} seconds"); this.output.WriteLine($"Parse time: {parseTime / 1000.0} seconds"); this.output.WriteLine($"Convert time: {convertTime / 1000.0} seconds"); }
public void EfficientText() { string json = File.ReadAllText(DeserializePerfTests.LargeFileName); Stopwatch timer = Stopwatch.StartNew(); long parseTime = 0; long convertTime = 0; for (int i = 0; i < 4; i++) { Stopwatch innerTimer = Stopwatch.StartNew(); JsonValue value = JsonValue.StringToValue(json); parseTime += innerTimer.ElapsedMilliseconds; innerTimer.Restart(); BabyNames names = value.ToObject <BabyNames>(); convertTime += innerTimer.ElapsedMilliseconds; GC.KeepAlive(value); GC.KeepAlive(names); } long totalTime = timer.ElapsedMilliseconds; this.output.WriteLine($"Total time: {totalTime / 1000.0} seconds"); this.output.WriteLine($"Parse time: {parseTime / 1000.0} seconds"); this.output.WriteLine($"Convert time: {convertTime / 1000.0} seconds"); }
public void EfficientDirectDeserializeStream() { long oldTime = 0; Stopwatch timer = Stopwatch.StartNew(); for (int i = 0; i < 4; i++) { using (StreamReader reader = new StreamReader(DeserializePerfTests.LargeFileName, detectEncodingFromByteOrderMarks: true)) { BabyNames names = JsonValue.StringToObject <BabyNames>(reader); GC.KeepAlive(names); } long newTime = timer.ElapsedMilliseconds; this.output.WriteLine($"Iteration {i + 1}: {(newTime - oldTime) / 1000.0} seconds"); oldTime = newTime; } long totalTime = timer.ElapsedMilliseconds; this.output.WriteLine($"Total time: {totalTime / 1000.0} seconds"); }
public void NewtonsoftDirectDeserializeStream() { long oldTime = 0; Stopwatch timer = Stopwatch.StartNew(); JsonSerializer serializer = JsonSerializer.Create(); for (int i = 0; i < 4; i++) { using (StreamReader reader = new StreamReader(DeserializePerfTests.LargeFileName, detectEncodingFromByteOrderMarks: true)) using (JsonTextReader jsonReader = new JsonTextReader(reader)) { BabyNames names = serializer.Deserialize <BabyNames>(jsonReader); GC.KeepAlive(names); } long newTime = timer.ElapsedMilliseconds; this.output.WriteLine($"Iteration {i + 1}: {(newTime - oldTime) / 1000.0} seconds"); oldTime = newTime; } long totalTime = timer.ElapsedMilliseconds; this.output.WriteLine($"Total time: {totalTime / 1000.0} seconds"); }