Last active
January 30, 2021 11:08
-
-
Save dmitry-ra/f1b132d1e36ae3ae2a3aa4d647907815 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
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