Created
January 14, 2023 02:25
-
-
Save Stovoy/04229a08b1caf699ecec69597426344f 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
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using CommunityToolkit.HighPerformance; | |
public struct Position { | |
public float x; | |
public float y; | |
}; | |
public class TestStructModification { | |
public delegate void ActionRef<T>(ref T item); | |
public List<Position> positions; | |
[GlobalSetup] | |
public void Setup() { | |
positions = new List<Position>(); | |
for (var i = 0; i < 100_000; i++) { | |
positions.Add(new Position { x = i, y = i }); | |
} | |
} | |
[Benchmark] | |
public void NewWithIncrement() { | |
Entrypoint(NewWithIncrement); | |
} | |
[Benchmark] | |
public void NewWithAddition() { | |
Entrypoint(NewWithAddition); | |
} | |
[Benchmark] | |
public void UpdateInPlace() { | |
Entrypoint(UpdateInPlace); | |
} | |
public void Entrypoint(ActionRef<Position> action) { | |
var positionsSpan = positions.AsSpan(); | |
for (var i = 0; i < positions.Count; i++) { | |
action(ref positionsSpan[i]); | |
} | |
} | |
public void NewWithIncrement(ref Position position) { | |
position = new Position { | |
x = position.x++, | |
y = position.y++ | |
}; | |
} | |
public void NewWithAddition(ref Position position) { | |
position = new Position { | |
x = position.x + 1, | |
y = position.y + 1, | |
}; | |
} | |
public void UpdateInPlace(ref Position position) { | |
position.x += 1; | |
position.y += 1; | |
} | |
} | |
public class Program { | |
public static void Main(string[] args) { | |
BenchmarkRunner.Run<TestStructModification>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmark written for genaray/Arch#49
Very surprising results: