This commit is contained in:
anramanath 2021-08-02 13:18:58 -07:00
Родитель f0440eef0d
Коммит bcd67d0d80
2 изменённых файлов: 59 добавлений и 0 удалений

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

@ -30,6 +30,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
@ -37,6 +38,7 @@
<ItemGroup>
<ProjectReference Include="..\Microsoft.Performance.SDK.Runtime.NetCoreApp\Microsoft.Performance.SDK.Runtime.NetCoreApp.csproj" />
<ProjectReference Include="..\Microsoft.Performance.SDK.Runtime.Tests\Microsoft.Performance.SDK.Runtime.Tests.csproj" />
<ProjectReference Include="..\Microsoft.Performance.SDK.Runtime\Microsoft.Performance.SDK.Runtime.csproj" />
<ProjectReference Include="..\Microsoft.Performance.SDK\Microsoft.Performance.SDK.csproj" />
<ProjectReference Include="..\Microsoft.Performance.Testing.SDK\Microsoft.Performance.Testing.SDK.csproj" />

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

@ -0,0 +1,57 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
/// <summary>
/// Sanity Check Benchmark testing to see if IEnumerable is more efficient than using a list.
/// <see cref="BaseSourceProcessingSession.ProcessDataElement(T, TContext, System.Threading.CancellationToken)"/>
/// </summary>
public class ProcessForLoopEfficiency
{
private const int NumCookers = 1000;
private readonly List<int> _list;
private readonly IEnumerable<int> _enumerable;
public ProcessForLoopEfficiency()
{
_list = new List<int>();
for (int cooker = 0; cooker < NumCookers; cooker++)
{
// create a new ISourceDataCooker
//TestSourceDataCooker cooker = new TestSourceDataCooker();
_list.Add(cooker);
}
_enumerable = (IEnumerable<int>)_list;
}
[Benchmark]
public void EnumerableProcessDataCookers()
{
foreach (var cooker in _enumerable)
{
var newCooker = cooker;
var anotherCooker = newCooker;
}
}
[Benchmark]
public void ListProcessDataCookers()
{
for (int cookerIdx = 0; cookerIdx < _list.Count; cookerIdx++)
{
var sourceDataCooker = _list[cookerIdx];
var newCooker = sourceDataCooker;
var anotherCooker = newCooker;
}
}
static void Main(string[] args)
{
BenchmarkRunner.Run<ProcessForLoopEfficiency>();
}
}