Created
August 19, 2016 13:26
-
-
Save mattwarren/e6856ab4625d4e306cc04b9349edd869 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
[Config(typeof(Config))] | |
public class StringSerialisationDeserialisation | |
{ | |
static void Main(string[] args) | |
{ | |
var test = new StringSerialisationDeserialisation(); | |
var normalWrite = test.NormalWrite(); | |
Console.WriteLine("Normal write: length={0}, \"{1}\"", normalWrite.Length, | |
Encoding.UTF8.GetString(normalWrite.GetBuffer(), 0, (int)normalWrite.Length)); | |
Console.WriteLine("Normal read: {0}", test.NormalRead()); | |
var optimisedWrite = test.OptimisedWrite(); | |
Console.WriteLine("Optimised write: length={0} ({1}), \"{2}\"", | |
optimisedWrite.Length, optimisedWrite.GetBuffer()[0], | |
Encoding.UTF8.GetString(optimisedWrite.GetBuffer(), 1, (int)optimisedWrite.Length - 1)); | |
Console.WriteLine("Optimised read: {0}", test.OptimisedRead()); | |
var summary = BenchmarkRunner.Run<StringSerialisationDeserialisation>(); | |
} | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
Add(Job.Clr.WithLaunchCount(1).WithWarmupCount(5).WithTargetCount(5)); | |
Add(new MemoryDiagnoser()); | |
Add(JitOptimizationsValidator.FailOnError); | |
Add(RPlotExporter.Default); | |
} | |
} | |
private static string textToWriteShort = "Lorem ipsum gravida habitant fames senectus."; | |
private static string textToWriteLong = "Lorem ipsum quis per at sit sollicitudin, proin pharetra vivamus maecenas vitae, lacus senectus curabitur ad donec."; | |
private static Serializer serialiser = new Serializer(); | |
private static SerializerSession serializerSession = new SerializerSession(serialiser); | |
private static DeserializerSession deserializerSession = new DeserializerSession(serialiser); | |
private static StringSerializer stringSerialiser = StringSerializer.Instance; | |
private static MemoryStream streamNormalWrite, streamNormalRead; | |
private static MemoryStream streamOptimisedWrite, streamOptimisedRead; | |
static StringSerialisationDeserialisation() | |
{ | |
// Writing | |
streamNormalWrite = new MemoryStream(1024); | |
streamOptimisedWrite = new MemoryStream(1024); | |
// Reading | |
var normalSerialisedText = Encoding.UTF8.GetBytes(textToWriteShort); | |
streamNormalRead = new MemoryStream(normalSerialisedText, 0, normalSerialisedText.Length, writable: false, publiclyVisible: true); | |
var tempStream = new MemoryStream(); | |
stringSerialiser.WriteValue(tempStream, textToWriteShort, serializerSession); | |
byte[] wireSerialisedText = new byte[tempStream.Length]; | |
Array.Copy(tempStream.GetBuffer(), wireSerialisedText, tempStream.Length); | |
streamOptimisedRead = new MemoryStream(wireSerialisedText); | |
} | |
[Benchmark] | |
public MemoryStream NormalWrite() | |
{ | |
streamNormalWrite.Position = 0; | |
var bytes = Encoding.UTF8.GetBytes(textToWriteShort); | |
streamNormalWrite.Write(bytes, 0, bytes.Length); | |
return streamNormalWrite; | |
} | |
[Benchmark] | |
public MemoryStream OptimisedWrite() | |
{ | |
streamOptimisedWrite.Position = 0; | |
stringSerialiser.WriteValue(streamOptimisedWrite, textToWriteShort, serializerSession); | |
return streamOptimisedWrite; | |
} | |
[Benchmark] | |
public string NormalRead() | |
{ | |
return Encoding.UTF8.GetString(streamNormalRead.GetBuffer(), 0, (int)streamNormalRead.Length); | |
} | |
[Benchmark] | |
public string OptimisedRead() | |
{ | |
streamOptimisedRead.Position = 0; | |
//return (string)stringSerialiser.ReadValue(streamOptimisedRead, deserializerSession); | |
return StringSerializer.ReadValueImpl(streamOptimisedRead, deserializerSession); | |
} | |
} |
Host Process Environment Information:
BenchmarkDotNet.Core=v0.9.9.0
OS=Microsoft Windows NT 6.1.7601 Service Pack 1
Processor=Intel(R) Core(TM) i7-4800MQ CPU 2.70GHz, ProcessorCount=8
Frequency=2630761 ticks, Resolution=380.1181 ns, Timer=TSC
CLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE
GC=Concurrent Workstation
JitModules=clrjit-v4.6.1076.0
Type=StringSerialisationDeserialisation Mode=Throughput Toolchain=Clr
Runtime=Clr LaunchCount=1 WarmupCount=5
TargetCount=5
Method | Median | StdDev | Gen 0 | Gen 1 | Gen 2 | Bytes Allocated/Op |
---|---|---|---|---|---|---|
NormalWrite | 121.9761 ns | 8.9020 ns | 428.03 | - | - | 19.87 |
OptimisedWrite | 112.4477 ns | 0.5268 ns | - | - | - | 0.01 |
NormalRead | 66.2142 ns | 0.8424 ns | 802.19 | - | - | 37.00 |
OptimisedRead | 95.5270 ns | 1.5711 ns | 777.86 | - | - | 35.95 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: