by Anonymous » 17 Aug 2025, 21:33
Ich arbeite mit einigen Benchmarks und bemerkte ein seltsames Verhalten mit meinen Methoden im Vergleich zu Linq. Testen auf .NET 9, x64 ryujit. Beim Testen schrieb ich eine vereinfachte Version von Linqs Max (Decimal) , mit der die Typ-Überprüfung der Typ-Überprüfung entfernt wird. Ansonsten ist eine genaue Kopie aus der Quelle.private decimal[] _array = default!;
[GlobalSetup]
public void GlobalSetup()
{
_array = new decimal[100];
for (int i = 0; i < _array.Length; i++)
_array = (decimal)i;
}
[Benchmark]
public decimal MyMax()
{
ReadOnlySpan span = _array;
if (span.IsEmpty)
throw new ArgumentException();
decimal value = span[0];
for (int i = 1; (uint)i < (uint)span.Length; i++)
{
if (span > value)
{
value = span;
}
}
return value;
}
[Benchmark]
public decimal Linq()
{
return _array.Max();
}
< /code>
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 9.0.304
[Host] : .NET 9.0.8 (9.0.825.36511), X64 RyuJIT AVX2
DefaultJob : .NET 9.0.8 (9.0.825.36511), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Code Size | Allocated |
|------- |---------:|--------:|--------:|----------:|----------:|
| MyMax | 498.7 ns | 3.63 ns | 3.03 ns | 578 B | - |
| Linq | 446.2 ns | 4.48 ns | 3.97 ns | 759 B | - |
< /code>
Just to ensure this wasn't some sort of JIT side effect, I tried putting the Linq method before MyMax so that Linq would be benchmarked first, and results were the same: Linq is still faster than MyMax.
I admit, I'm really bad at assembly, so I can't really determine anything looking at the disassembly. But, regardless, considering I'm using the same C# code as Linq, even if I was able to find what was different I still wouldn't know why it happened.
I know it's not a huge difference, but more than anything, I just really wonder, academically, why it's happening. Can someone enlighten me? I sure hope it's something simple and obvious I'm missing.
Thanks in advance!
Ich arbeite mit einigen Benchmarks und bemerkte ein seltsames Verhalten mit meinen Methoden im Vergleich zu Linq. Testen auf .NET 9, x64 ryujit. Beim Testen schrieb ich eine vereinfachte Version von Linqs Max (Decimal) , mit der die Typ-Überprüfung der Typ-Überprüfung entfernt wird. Ansonsten ist eine genaue Kopie aus der Quelle.private decimal[] _array = default!;
[GlobalSetup]
public void GlobalSetup()
{
_array = new decimal[100];
for (int i = 0; i < _array.Length; i++)
_array[i] = (decimal)i;
}
[Benchmark]
public decimal MyMax()
{
ReadOnlySpan span = _array;
if (span.IsEmpty)
throw new ArgumentException();
decimal value = span[0];
for (int i = 1; (uint)i < (uint)span.Length; i++)
{
if (span[i] > value)
{
value = span[i];
}
}
return value;
}
[Benchmark]
public decimal Linq()
{
return _array.Max();
}
< /code>
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 9.0.304
[Host] : .NET 9.0.8 (9.0.825.36511), X64 RyuJIT AVX2
DefaultJob : .NET 9.0.8 (9.0.825.36511), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Code Size | Allocated |
|------- |---------:|--------:|--------:|----------:|----------:|
| MyMax | 498.7 ns | 3.63 ns | 3.03 ns | 578 B | - |
| Linq | 446.2 ns | 4.48 ns | 3.97 ns | 759 B | - |
< /code>
Just to ensure this wasn't some sort of JIT side effect, I tried putting the Linq method before MyMax so that Linq would be benchmarked first, and results were the same: Linq is still faster than MyMax.
I admit, I'm really bad at assembly, so I can't really determine anything looking at the disassembly. But, regardless, considering I'm using the same C# code as Linq, even if I was able to find what was different I still wouldn't know why it happened.
I know it's not a huge difference, but more than anything, I just really wonder, academically, why it's happening. Can someone enlighten me? I sure hope it's something simple and obvious I'm missing.
Thanks in advance!