Skip to content

Instantly share code, notes, and snippets.

@MeilCli
Last active June 17, 2018 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeilCli/f18934de4759ec38a5365b529c553dd3 to your computer and use it in GitHub Desktop.
Save MeilCli/f18934de4759ec38a5365b529c553dd3 to your computer and use it in GitHub Desktop.
stackalloc戦略のパフォーマンス確認
BenchmarkDotNet=v0.10.14, OS=Windows 10.0.16299.492 (1709/FallCreatorsUpdate/Redstone3)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328123 Hz, Resolution=300.4697 ns, Timer=TSC
.NET Core SDK=2.1.300
  [Host]     : .NET Core 2.0.7 (CoreCLR 4.6.26328.01, CoreFX 4.6.26403.03), 64bit RyuJIT
  Job-ANIBAB : .NET Core 2.0.7 (CoreCLR 4.6.26328.01, CoreFX 4.6.26403.03), 64bit RyuJIT
  Job-TCMRRU : .NET Core 2.1.0 (CoreCLR 4.6.26515.07, CoreFX 4.6.26515.06), 64bit RyuJIT
  Clr        : .NET Framework 4.7.1 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.2671.0

Method Job Runtime Toolchain Mean Error StdDev Min Max Gen 0 Allocated
NormalIndexOf Default Core .NET Core 2.0 7.289 ns 0.0645 ns 0.0539 ns 7.208 ns 7.400 ns 0.0114 48 B
SpanIndexOf Default Core .NET Core 2.0 32.658 ns 0.1579 ns 0.1318 ns 32.301 ns 32.802 ns - 0 B
NormalMax8 Default Core .NET Core 2.0 15.576 ns 0.0557 ns 0.0494 ns 15.484 ns 15.664 ns 0.0133 56 B
SpanMax8 Default Core .NET Core 2.0 44.329 ns 0.2466 ns 0.2307 ns 43.898 ns 44.609 ns - 0 B
NormalMax16 Default Core .NET Core 2.0 20.003 ns 0.0668 ns 0.0625 ns 19.884 ns 20.117 ns 0.0210 88 B
SpanMax16 Default Core .NET Core 2.0 49.677 ns 0.2953 ns 0.2762 ns 49.182 ns 50.033 ns - 0 B
NormalIndexOf Default Core .NET Core 2.1 5.979 ns 0.0319 ns 0.0299 ns 5.923 ns 6.031 ns 0.0114 48 B
SpanIndexOf Default Core .NET Core 2.1 5.600 ns 0.0141 ns 0.0132 ns 5.575 ns 5.620 ns - 0 B
NormalMax8 Default Core .NET Core 2.1 12.207 ns 0.0320 ns 0.0284 ns 12.165 ns 12.259 ns 0.0133 56 B
SpanMax8 Default Core .NET Core 2.1 8.124 ns 0.0711 ns 0.0665 ns 8.022 ns 8.206 ns - 0 B
NormalMax16 Default Core .NET Core 2.1 14.532 ns 0.1493 ns 0.1247 ns 14.362 ns 14.847 ns 0.0210 88 B
SpanMax16 Default Core .NET Core 2.1 10.113 ns 0.2148 ns 0.2010 ns 9.786 ns 10.563 ns - 0 B
NormalIndexOf Clr Clr Default 8.024 ns 0.0679 ns 0.0530 ns 7.903 ns 8.085 ns 0.0114 48 B
SpanIndexOf Clr Clr Default 23.678 ns 0.3477 ns 0.3253 ns 23.140 ns 24.295 ns - 0 B
NormalMax8 Clr Clr Default 13.129 ns 0.1112 ns 0.0929 ns 12.938 ns 13.292 ns 0.0133 56 B
SpanMax8 Clr Clr Default 41.417 ns 0.1957 ns 0.1830 ns 41.052 ns 41.778 ns - 0 B
NormalMax16 Clr Clr Default 14.360 ns 0.1311 ns 0.1162 ns 14.187 ns 14.613 ns 0.0210 88 B
SpanMax16 Clr Clr Default 46.676 ns 0.3485 ns 0.3260 ns 46.193 ns 47.291 ns - 0 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Columns;
using BenchmarkDotNet.Attributes.Jobs;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Toolchains.CsProj;
using System;
namespace Bench
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly)]
public class Core20JobAttribute : Attribute, IConfigSource
{
public Core20JobAttribute()
{
Config = ManualConfig.CreateEmpty().With(Job.Core.With(CsProjCoreToolchain.NetCoreApp20));
}
public IConfig Config { get; }
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly)]
public class Core21JobAttribute : Attribute, IConfigSource
{
public Core21JobAttribute()
{
Config = ManualConfig.CreateEmpty().With(Job.Core.With(CsProjCoreToolchain.NetCoreApp21));
}
public IConfig Config { get; }
}
[ClrJob, Core20Job, Core21Job]
[MeanColumn, MinColumn, MaxColumn]
[MemoryDiagnoser]
public class StackallocBench
{
[Benchmark]
public int NormalIndexOf()
{
int[] ar = new[] { 1, 4, 7, 8, 0, 1 };
for (int i = 0; i < ar.Length; i++)
{
if (ar[i] == 0)
{
return i;
}
}
return -1;
}
[Benchmark]
public int SpanIndexOf()
{
Span<int> ar = stackalloc[] { 1, 4, 7, 8, 0, 1 };
for (int i = 0; i < ar.Length; i++)
{
if (ar[i] == 0)
{
return i;
}
}
return -1;
}
public readonly struct Size8
{
public readonly int A, B;
public Size8(int a, int b)
{
A = a;
B = b;
}
}
public readonly struct Size16
{
public readonly int A, B, C, D;
public Size16(int a, int b, int c, int d)
{
A = a;
B = b;
C = c;
D = d;
}
}
[Benchmark]
public Size8 NormalMax8()
{
Span<Size8> ar = new[] {
new Size8(1, 2),
new Size8(2, 2),
new Size8(3, 3),
new Size8(1, 1)
};
Size8 max = ar[0];
for (int i = 1; i < ar.Length; i++)
{
Size8 x = ar[i];
if (max.A + max.B < x.A + x.B)
{
max = x;
}
}
return max;
}
[Benchmark]
public Size8 SpanMax8()
{
Span<Size8> ar = stackalloc[] {
new Size8(1, 2),
new Size8(2, 2),
new Size8(3, 3),
new Size8(1, 1)
};
ref Size8 max = ref ar[0];
for (int i = 1; i < ar.Length; i++)
{
ref Size8 x = ref ar[i];
if (max.A + max.B < x.A + x.B)
{
max = ref x;
}
}
return max;
}
[Benchmark]
public Size16 NormalMax16()
{
Span<Size16> ar = new[] {
new Size16(1, 2, 3, 4),
new Size16(2, 2, 2, 2),
new Size16(3, 3, 2, 2),
new Size16(1, 1, 2, 2)
};
Size16 max = ar[0];
for (int i = 1; i < ar.Length; i++)
{
Size16 x = ar[i];
if (max.A + max.B + max.C + max.D < x.A + x.B + x.C + x.D)
{
max = x;
}
}
return max;
}
[Benchmark]
public Size16 SpanMax16()
{
Span<Size16> ar = stackalloc[] {
new Size16(1, 2, 3, 4),
new Size16(2, 2, 2, 2),
new Size16(3, 3, 2, 2),
new Size16(1, 1, 2, 2)
};
ref Size16 max = ref ar[0];
for (int i = 1; i < ar.Length; i++)
{
ref Size16 x = ref ar[i];
if (max.A + max.B + max.C + max.D < x.A + x.B + x.C + x.D)
{
max = ref x;
}
}
return max;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment