Deep profiler makes these head to head comparisons pretty useless.
It’s not a bad idea to try using the Performance Testing Package to experiment with this kind of thing.
using NUnit.Framework;
using System.Runtime.CompilerServices;
using Unity.PerformanceTesting;
using UnityEngine;
public class NewTestScript2
{
const int n = 1024 * 1024;
static Vector3 c { get; set; }
[Test, Performance]
public void Add_Vector3_Vector3_CustomOperator()
{
Measure.Method(static () =>
{
Vector3 a = new(1, 2, 3), b = new(4, 5, 6);
for (int i = 0; i < n; i++)
{
c = Add(a, b);
}
})
.WarmupCount(8)
.DynamicMeasurementCount()
.SampleGroup(new SampleGroup(nameof(Add_Vector3_Vector3_Operator), SampleUnit.Microsecond, false))
.Run();
static Vector3 Add(Vector3 a, Vector3 b)
{
return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
}
}
[Test, Performance]
public void Add_Vector3_Vector3_CustomOperator_AggressiveInlining()
{
Measure.Method(static () =>
{
Vector3 a = new(1, 2, 3), b = new(4, 5, 6);
for (int i = 0; i < n; i++)
{
c = Add(a, b);
}
})
.WarmupCount(8)
.DynamicMeasurementCount()
.SampleGroup(new SampleGroup(nameof(Add_Vector3_Vector3_Operator), SampleUnit.Microsecond, false))
.Run();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static Vector3 Add(Vector3 a, Vector3 b)
{
return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
}
}
[Test, Performance]
public void Add_Vector3_Vector3_Operator()
{
Measure.Method(static () =>
{
Vector3 a = new(1, 2, 3), b = new(4, 5, 6);
for (int i = 0; i < n; i++)
{
c = a + b;
}
})
.WarmupCount(8)
.DynamicMeasurementCount()
.SampleGroup(new SampleGroup(nameof(Add_Vector3_Vector3_Operator), SampleUnit.Microsecond, false))
.Run();
}
[Test, Performance]
public void Add_Vector3_Vector3_Componentwise()
{
Measure.Method(static () =>
{
Vector3 a = new(1, 2, 3), b = new(4, 5, 6);
for (int i = 0; i < n; i++)
{
c = new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
}
})
.WarmupCount(8)
.DynamicMeasurementCount()
.SampleGroup(new SampleGroup(nameof(Add_Vector3_Vector3_Componentwise), SampleUnit.Microsecond, false))
.Run();
}
[Test, Performance]
public void Add_Vector3_Components_Componentwise()
{
Measure.Method(() =>
{
Vector3 a = new(1, 2, 3);
float b0 = 4, b1 = 5, b2 = 6;
for (int i = 0; i < n; i++)
{
c = new Vector3(a.x + b0, a.y + b1, a.z + b2);
}
})
.WarmupCount(8)
.DynamicMeasurementCount()
.SampleGroup(new SampleGroup(nameof(Add_Vector3_Components_Componentwise), SampleUnit.Microsecond, false))
.Run();
}
}
Add_Vector3_Components_Componentwise in Microseconds
Min: 6259.30 μs
Median: 6284.10 μs
Max: 6348.30 μs
Avg: 6288.89 μs
StdDev: 26.69 μs
SampleCount: 9
Sum: 56600.00 μs
Add_Vector3_Vector3_Componentwise in Microseconds
Min: 6266.30 μs
Median: 6282.20 μs
Max: 6300.80 μs
Avg: 6285.38 μs
StdDev: 12.41 μs
SampleCount: 9
Sum: 56568.40 μs
Add_Vector3_Vector3_CustomOperator in Microseconds
Min: 18952.40 μs
Median: 19220.10 μs
Max: 19467.30 μs
Avg: 19190.06 μs
StdDev: 147.02 μs
SampleCount: 9
Sum: 172710.50 μs
Add_Vector3_Vector3_CustomOperator_AggressiveInlining in Microseconds
Min: 9284.20 μs
Median: 9304.90 μs
Max: 9355.20 μs
Avg: 9310.80 μs
StdDev: 22.99 μs
SampleCount: 9
Sum: 83797.20 μs
Add_Vector3_Vector3_Operator in Microseconds
Min: 9303.00 μs
Median: 9335.90 μs
Max: 9376.10 μs
Avg: 9333.42 μs
StdDev: 25.71 μs
SampleCount: 9
Sum: 84000.80 μs