Skip to content

Commit

Permalink
Benchmarked performance when flipping bytes for endianness.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Nov 15, 2024
1 parent deacbca commit 6280f93
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
89 changes: 89 additions & 0 deletions Benchmarks/EndiannessComparison.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;

using BenchmarkDotNet.Attributes;

using CommunityToolkit.HighPerformance;

using schema.binary;


namespace benchmarks;

[MemoryDiagnoser]
public class EndiannessComparison {
public const int n = 10000;

[Params(Endianness.BigEndian, Endianness.LittleEndian)]
public Endianness Endianness { get; set; }

public uint[] values_ = new uint[n];

[Benchmark]
public void ReverseSpansManually() {
var span = this.values_.AsSpan().AsBytes();
for (var i = 0; i < span.Length; i++) {
span.Slice(4 * i, 4).Reverse();
}
}

[Benchmark]
public void ReverseSpansUsingReverser() {
var reverser = new SpanElementReverser();

var span = this.values_.AsSpan().AsBytes();
reverser.ReverseElements(span, 4);
}

[Benchmark]
public void ReverseSpansUsingReverserInterface() {
ISpanElementReverser reverser = new SpanElementReverser();

var span = this.values_.AsSpan().AsBytes();
reverser.ReverseElements(span, 4);
}

[Benchmark]
public void ReverseSpansUsingReverserNoop() {
var reverser = new NoopSpanElementReverser();

var span = this.values_.AsSpan().AsBytes();
reverser.ReverseElements(span, 4);
}

[Benchmark]
public void ReverseSpansUsingReverserNoopInterface() {
ISpanElementReverser reverser = new NoopSpanElementReverser();

var span = this.values_.AsSpan().AsBytes();
reverser.ReverseElements(span, 4);
}

[Benchmark]
public void ReverseSpansAsUintsInlined() {
var span = this.values_.AsSpan();
for (var i = 0; i < span.Length; i++) {
var value = span[i];
span[i] = (value & 0x000000FFU) << 24 |
(value & 0x0000FF00U) << 8 |
(value & 0x00FF0000U) >> 8 |
(value & 0xFF000000U) >> 24;
}
}

[Benchmark]
public void ReverseSpansAsUintsViaMethod() {
var span = this.values_.AsSpan();
for (var i = 0; i < span.Length; i++) {
span[i] = ReverseBytes(span[i]);
}
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReverseBytes(uint value) {
return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
(value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
}
}
2 changes: 1 addition & 1 deletion Benchmarks/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace benchmarks;

public class Program {
public static void Main(string[] args) {
var summary = BenchmarkRunner.Run<ReadingValues>(
var summary = BenchmarkRunner.Run<EndiannessComparison>(
ManualConfig
.Create(DefaultConfig.Instance)
.AddDiagnoser(
Expand Down

0 comments on commit 6280f93

Please sign in to comment.