This commit is contained in:
Andrey Akinshin 2013-08-26 22:34:16 +07:00
Родитель 86aadad9d7
Коммит 5830ef2293
3 изменённых файлов: 45 добавлений и 1 удалений

Просмотреть файл

@ -42,6 +42,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="IncrementProgram.cs" />
<Compile Include="MakeRefVsBoxingProgram.cs" />
<Compile Include="MultidimensionalArrayProgram.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

Просмотреть файл

@ -0,0 +1,42 @@
using BenchmarkDotNet;
namespace Benchmarks
{
public class MakeRefVsBoxingProgram
{
private const int IterationCount = 100000000;
private int[] array;
public void Run()
{
array = new int[5];
var competition = new BenchmarkCompetition();
competition.AddTask("MakeRef", MakeRef);
competition.AddTask("Boxing", Boxing);
competition.Run();
}
public void MakeRef()
{
for (int i = 0; i < IterationCount; i++)
Set1(array, 0, i);
}
public void Boxing()
{
for (int i = 0; i < IterationCount; i++)
Set2(array, 0, i);
}
public void Set1<T>(T[] a, int i, int v)
{
__refvalue(__makeref(a[i]), int) = v;
}
public void Set2<T>(T[] a, int i, int v)
{
a[i] = (T)(object)v;
}
}
}

Просмотреть файл

@ -11,7 +11,8 @@ namespace Benchmarks
new ProgramRunner("MultidimensionalArray", () => new MultidimensionalArrayProgram().Run()),
new ProgramRunner("StaticField", () => new StaticFieldProgram().Run()),
new ProgramRunner("ShiftVsMultiply", () => new ShiftVsMultiplyProgram().Run()),
new ProgramRunner("ReverseSort", () => new ReverseSortProgram().Run()),
new ProgramRunner("ReverseSort", () => new ReverseSortProgram().Run()),
new ProgramRunner("MakeRefVsBoxing", () => new MakeRefVsBoxingProgram().Run()),
};
static void Main(string[] args)