Skip to content

Instantly share code, notes, and snippets.

@dmitry-ra
Last active January 30, 2021 11:08
Show Gist options
  • Save dmitry-ra/f1b132d1e36ae3ae2a3aa4d647907815 to your computer and use it in GitHub Desktop.
Save dmitry-ra/f1b132d1e36ae3ae2a3aa4d647907815 to your computer and use it in GitHub Desktop.
namespace Base64Benchmarks
{
public class Base64EncodeBenchmarks
{
private const int N = 20 * 1024 * 1024;
private readonly byte[] _data;
public Base64EncodeBenchmarks()
{
_data = new byte[N];
new Random(42).NextBytes(_data);
}
[Benchmark]
public void System_Base64_EncodeToUtf8()
{
var output = new byte[4 * 1024];
for (var i = 0; i < _data.Length;)
{
Base64.EncodeToUtf8(new ReadOnlySpan<byte>(_data, i, _data.Length - i), output, out var bytesConsumed,
out var bytesWritten);
i += bytesConsumed;
}
}
[Benchmark]
public void NaiveLoad()
{
for (var i = 0; i < _data.Length; i++)
{
_ = _data[i];
}
}
[Benchmark]
public unsafe void AvxLoad()
{
fixed (byte* ptr = _data)
{
var pData = ptr;
var pEnd = ptr + _data.Length;
while (pData < pEnd)
{
Avx.LoadVector256(pData);
pData += 8;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment