Created
April 27, 2025 13:28
-
-
Save ZedDevStuff/f4fdd74cb7a15b1c0d080779bacf3b9c to your computer and use it in GitHub Desktop.
MemoryPack vs MessagePack benchmark
This file contains hidden or 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.Numerics; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using MemoryPack; | |
using MessagePack; | |
public partial class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
BenchmarkRunner.Run<Benchmark>(); | |
} | |
public class Benchmark | |
{ | |
[Params(10_000)] | |
public int Count { get; set; } | |
public ExampleClass[] ClassListInput { get; set; } = []; | |
public List<byte[]> ClassListOutput { get; set; } = new List<byte[]>(); | |
public ExampleStruct[] StructListInput { get; set; } = []; | |
public List<byte[]> StructListOutput { get; set; } = new List<byte[]>(); | |
[IterationSetup(Targets = ["MemoryPackSerializeClass", "MessagePackSerializeClass", "MemoryPackSerializeStruct", "MessagePackSerializeStruct"])] | |
public void Setup() | |
{ | |
ClassListInput = new ExampleClass[Count]; | |
StructListInput = new ExampleStruct[Count]; | |
Random random1 = new Random(0); | |
Random random2 = new Random(0); | |
for (int i = 0; i < Count; i++) | |
{ | |
ClassListInput[i] = ExampleClass.Random(random1); | |
StructListInput[i] = ExampleStruct.Random(random2); | |
} | |
} | |
[IterationSetup(Targets = [nameof(MemoryPackDeserializeClass), nameof(MemoryPackDeserializeStruct)])] | |
public void SetupDeserializeMemorypack() | |
{ | |
ClassListOutput = new List<byte[]>(Count); | |
StructListOutput = new List<byte[]>(Count); | |
Random random1 = new Random(0); | |
Random random2 = new Random(0); | |
for (int i = 0; i < Count; i++) | |
{ | |
ClassListOutput.Add(MemoryPackSerializer.Serialize(ExampleClass.Random(random1))); | |
StructListOutput.Add(MemoryPackSerializer.Serialize(ExampleStruct.Random(random2))); | |
} | |
} | |
[IterationSetup(Targets = [nameof(MessagePackDeserializeClass), nameof(MessagePackDeserializeStruct)])] | |
public void SetupDeserializeMessagepack() | |
{ | |
ClassListOutput = new List<byte[]>(Count); | |
StructListOutput = new List<byte[]>(Count); | |
Random random1 = new Random(0); | |
Random random2 = new Random(0); | |
for (int i = 0; i < Count; i++) | |
{ | |
ClassListOutput.Add(MessagePackSerializer.Serialize(ExampleClass.Random(random1))); | |
StructListOutput.Add(MessagePackSerializer.Serialize(ExampleStruct.Random(random2))); | |
} | |
} | |
// Serialize | |
[Benchmark] | |
public void MemoryPackSerializeClass() | |
{ | |
for(int i = 0;i < Count; i++) | |
{ | |
MemoryPackSerializer.Serialize(ClassListInput[i]); | |
} | |
} | |
[Benchmark] | |
public void MessagePackSerializeClass() | |
{ | |
for (int i = 0; i < Count; i++) | |
{ | |
MessagePackSerializer.Serialize(ClassListInput[i]); | |
} | |
} | |
[Benchmark] | |
public void MemoryPackSerializeStruct() | |
{ | |
for (int i = 0; i < Count; i++) | |
{ | |
MemoryPackSerializer.Serialize(StructListInput[i]); | |
} | |
} | |
[Benchmark] | |
public void MessagePackSerializeStruct() | |
{ | |
for (int i = 0; i < Count; i++) | |
{ | |
MessagePackSerializer.Serialize(StructListInput[i]); | |
} | |
} | |
// Deserialize | |
[Benchmark] | |
public void MemoryPackDeserializeClass() | |
{ | |
for (int i = 0; i < Count; i++) | |
{ | |
MemoryPackSerializer.Deserialize<ExampleClass>(ClassListOutput[i]); | |
} | |
} | |
[Benchmark] | |
public void MessagePackDeserializeClass() | |
{ | |
for (int i = 0; i < Count; i++) | |
{ | |
MessagePackSerializer.Deserialize<ExampleClass>(ClassListOutput[i]); | |
} | |
} | |
[Benchmark] | |
public void MemoryPackDeserializeStruct() | |
{ | |
for (int i = 0; i < Count; i++) | |
{ | |
MemoryPackSerializer.Deserialize<ExampleStruct>(StructListOutput[i]); | |
} | |
} | |
[Benchmark] | |
public void MessagePackDeserializeStruct() | |
{ | |
for (int i = 0; i < Count; i++) | |
{ | |
MessagePackSerializer.Deserialize<ExampleStruct>(StructListOutput[i]); | |
} | |
} | |
} | |
[MemoryPackable, MessagePackObject(keyAsPropertyName: true)] | |
public partial class ExampleClass | |
{ | |
public int OwnerId { get; set; } | |
public string OwnerName { get; set; } | |
public Vector2 Position { get; set; } | |
public Vector2 Velocity { get; set; } | |
public float Rotation { get; set; } | |
public static ExampleClass Random(Random? random = null) | |
{ | |
random ??= System.Random.Shared; | |
return new ExampleClass | |
{ | |
OwnerId = random.Next(0, 1000), | |
OwnerName = random.NextString(10), | |
Position = new Vector2(random.Next(0, 100), random.Next(0, 100)), | |
Velocity = new Vector2(random.Next(0, 100), random.Next(0, 100)), | |
Rotation = (float)(random.NextDouble() * 360.0), | |
}; | |
} | |
} | |
[MemoryPackable, MessagePackObject(keyAsPropertyName: true)] | |
public partial struct ExampleStruct | |
{ | |
public int OwnerId { get; set; } | |
public string OwnerName { get; set; } | |
public Vector2 Position { get; set; } | |
public Vector2 Velocity { get; set; } | |
public float Rotation { get; set; } | |
public static ExampleStruct Random(Random? random = null) | |
{ | |
random ??= System.Random.Shared; | |
return new ExampleStruct | |
{ | |
OwnerId = random.Next(0, 1000), | |
OwnerName = random.NextString(10), | |
Position = new Vector2(random.Next(0, 100), random.Next(0, 100)), | |
Velocity = new Vector2(random.Next(0, 100), random.Next(0, 100)), | |
Rotation = (float)(random.NextDouble() * 360.0), | |
}; | |
} | |
} | |
} | |
public static class RandomExtensions | |
{ | |
public static string NextString(this Random random, int length) | |
{ | |
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
char[] buffer = new char[length]; | |
for (int i = 0; i < length; i++) | |
{ | |
buffer[i] = chars[random.Next(chars.Length)]; | |
} | |
return new string(buffer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment