Created
September 28, 2017 02:17
-
-
Save neuecc/25723720ee74b0f862cd7d0c1b50e968 to your computer and use it in GitHub Desktop.
additional benchmark
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
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using Utf8Json; | |
using Jil; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using BenchmarkDotNet.Attributes.Jobs; | |
namespace PerfBenchmark | |
{ | |
public struct MyModel | |
{ | |
public string X { get; set; } | |
public string Y { get; set; } | |
public int Z { get; set; } | |
} | |
public class JsonSerializeBench | |
{ | |
[Params(1, 10000)] | |
public int LoopNum { get; set; } | |
static string json; | |
static byte[] jsonBytes; | |
public JsonSerializeBench() | |
{ | |
var m = new MyModel(); | |
m.X = $"abcdefg"; | |
m.Y = $"@aiweraw"; | |
m.Z = int.MaxValue; | |
json = Jil.JSON.Serialize(m); | |
jsonBytes = Encoding.UTF8.GetBytes(json); | |
} | |
[Benchmark] | |
public void JilSerialize_String() | |
{ | |
var m = new MyModel(); | |
m.X = $"abcdefg"; | |
m.Y = $"@aiweraw"; | |
m.Z = int.MaxValue; | |
for (int i = 0; i < LoopNum; i++) | |
{ | |
Jil.JSON.Serialize(m); | |
} | |
} | |
[Benchmark] | |
public void JilSerialize_String_UTf8() | |
{ | |
var m = new MyModel(); | |
m.X = $"abcdefg"; | |
m.Y = $"@aiweraw"; | |
m.Z = int.MaxValue; | |
for (int i = 0; i < LoopNum; i++) | |
{ | |
Encoding.UTF8.GetBytes(Jil.JSON.Serialize(m)); | |
} | |
} | |
[Benchmark] | |
public void Utf8JsonSerialize() | |
{ | |
var m = new MyModel(); | |
m.X = $"abcdefg"; | |
m.Y = $"@aiweraw"; | |
m.Z = int.MaxValue; | |
for (int i = 0; i < LoopNum; i++) | |
{ | |
Utf8Json.JsonSerializer.Serialize(m); | |
} | |
} | |
[Benchmark] | |
public void JilJsonDeserialize_String() | |
{ | |
for (int i = 0; i < LoopNum; i++) | |
{ | |
Jil.JSON.Deserialize<MyModel>(json); | |
} | |
} | |
[Benchmark] | |
public void JilJsonDeserialize_Utf8() | |
{ | |
for (int i = 0; i < LoopNum; i++) | |
{ | |
Jil.JSON.Deserialize<MyModel>(Encoding.UTF8.GetString(jsonBytes)); | |
} | |
} | |
[Benchmark] | |
public void Utf8JsonDeserialize() | |
{ | |
for (int i = 0; i < LoopNum; i++) | |
{ | |
Utf8Json.JsonSerializer.Deserialize<MyModel>(jsonBytes); | |
} | |
} | |
} | |
} |
Author
neuecc
commented
Sep 28, 2017
Method | LoopNum | Mean | Error | StdDev |
---|---|---|---|---|
JilSerialize_String | 1 | 301.6 ns | 6.029 ns | 10.237 ns |
JilSerialize_String_UTf8 | 1 | 356.8 ns | 7.131 ns | 7.323 ns |
Utf8JsonSerialize | 1 | 153.1 ns | 1.726 ns | 1.614 ns |
JilJsonDeserialize_String | 1 | 285.8 ns | 3.786 ns | 3.541 ns |
JilJsonDeserialize_Utf8 | 1 | 332.8 ns | 4.157 ns | 3.889 ns |
Utf8JsonDeserialize | 1 | 267.5 ns | 3.077 ns | 2.878 ns |
JilSerialize_String | 10000 | 2,813,001.0 ns | 53,703.438 ns | 55,149.472 ns |
JilSerialize_String_UTf8 | 10000 | 3,464,489.1 ns | 37,125.605 ns | 34,727.311 ns |
Utf8JsonSerialize | 10000 | 1,446,431.1 ns | 19,472.401 ns | 18,214.495 ns |
JilJsonDeserialize_String | 10000 | 2,800,063.8 ns | 28,338.320 ns | 26,507.680 ns |
JilJsonDeserialize_Utf8 | 10000 | 3,284,278.1 ns | 58,104.968 ns | 51,508.536 ns |
Utf8JsonDeserialize | 10000 | 2,614,713.8 ns | 51,724.868 ns | 48,383.469 ns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment