Add ReverseSortProgram
This commit is contained in:
Родитель
21298c52e4
Коммит
86aadad9d7
|
@ -46,6 +46,7 @@
|
|||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ShiftVsMultiplyProgram.cs" />
|
||||
<Compile Include="ReverseSortProgram.cs" />
|
||||
<Compile Include="StaticFieldProgram.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
@ -11,6 +11,7 @@ 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()),
|
||||
};
|
||||
|
||||
static void Main(string[] args)
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using BenchmarkDotNet;
|
||||
|
||||
namespace Benchmarks
|
||||
{
|
||||
public class ReverseSortProgram
|
||||
{
|
||||
private const int N = 6000000, RandSeed = 123;
|
||||
private int[] originalData, data;
|
||||
|
||||
public void Run()
|
||||
{
|
||||
originalData = new int[N];
|
||||
var random = new Random(RandSeed);
|
||||
for (int i = 0; i < N; i++)
|
||||
originalData[i] = random.Next() % 50;
|
||||
|
||||
var competition = new BenchmarkCompetition();
|
||||
competition.AddTask("Linq", Initalize, LinqSort);
|
||||
competition.AddTask("CompareTo", Initalize, CompareToSort);
|
||||
competition.AddTask("(a,b)=>b-a", Initalize, ComparerMinusSort);
|
||||
competition.AddTask("Reverse", Initalize, ReverseSort);
|
||||
competition.Run();
|
||||
}
|
||||
|
||||
private void Initalize()
|
||||
{
|
||||
data = (int[])originalData.Clone();
|
||||
}
|
||||
|
||||
public void LinqSort()
|
||||
{
|
||||
data = data.OrderByDescending(a => a).ToArray();
|
||||
}
|
||||
|
||||
public void CompareToSort()
|
||||
{
|
||||
Array.Sort(data, (a, b) => a.CompareTo(b));
|
||||
}
|
||||
|
||||
public void ComparerMinusSort()
|
||||
{
|
||||
Array.Sort(data, (a, b) => b - a);
|
||||
}
|
||||
|
||||
public void ReverseSort()
|
||||
{
|
||||
Array.Sort(data);
|
||||
Array.Reverse(data);
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче