为什么这个自定义Vector2结构的性能比这个自定义Vector4差这么多?
c#
虽然标杆一些自定义矢量类型,我发现,我的意料,我Vector2类型是许多基本操作慢得多从阵列中读取时,比我的Vector4型(和的Vector3)尽管有代码本身更少的操作,字段和变量。这是一个大大简化的示例,演示了这一点:
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace VectorTest
{
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct TestStruct4
{
public float X, Y, Z, W;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TestStruct4(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TestStruct4 operator +(in TestStruct4 a, in TestStruct4 b)
{
return new TestStruct4(
a.X + b.X,
a.Y + b.Y,
a.Z + b.Z,
a.W + b.W);
}
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct TestStruct2
{
public float X, Y;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TestStruct2(float x, float y)
{
X = x;
Y = y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TestStruct2 operator +(in TestStruct2 a, in TestStruct2 b)
{
return new TestStruct2(
a.X + b.X,
a.Y + b.Y);
}
}
public class Program
{
private const int COUNT = 10000;
private static readonly TestStruct4[] s_arr4 = new TestStruct4[COUNT];
private static readonly TestStruct2[] s_arr2 = new TestStruct2[COUNT];
static unsafe void Main()
{
for(int i = 0; i < s_arr4.Length; i++)
s_arr4[i] = new TestStruct4(i, i * 2, i * 3, i * 4);
for(int i = 0; i < s_arr2.Length; i++)
s_arr2[i] = new TestStruct2(i, i * 2);
BenchmarkRunner.Run<Program>();
}
[Benchmark]
public TestStruct4 BenchmarkTestStruct4()
{
TestStruct4 ret = default;
for (int i = 0; i < COUNT; i++)
ret += s_arr4[i];
return ret;
}
[Benchmark]
public TestStruct2 BenchmarkTestStruct2()
{
TestStruct2 ret = default;
for (int i = 0; i < COUNT; i++)
ret += s_arr2[i];
return ret;
}
}
}
运行这个基准测试结果:
| 方法 | 意思 | 错误 | 标准差 |
|---|---|---|---|
| 基准测试结构4 | 9.863 美元 | 0.0706 美元 | 0.0626 美元 |
| 基准测试结构2 | 22.412我们 | 0.3100 美元 | 0.2899 美元 |
THE END
二维码