Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using BenchmarkDotNet.Attributes;

namespace Benchmark;

public class DictionaryConcatenateBenchmark
{
private Dictionary<int, string> FillData(int count)
{
var testValues = new Dictionary<int, string>();

for (int i = 0; i < count; i++)
{
testValues.Add(i, "value-" + i);
}

return testValues;
}

public IEnumerable<object[]> SampleData()
{
yield return new object[] { FillData(100), "100" };
yield return new object[] { FillData(1000), "1000" };
yield return new object[] { FillData(10000), "10000" };
}

[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public string WhenDictionaryUsingForEach(Dictionary<int, string> dictionaryData, string numberOfItems)
{
var result = string.Empty;

foreach (var testValue in dictionaryData)
{
result += testValue.Value;
}

return result;
}

[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public string WhenDictionaryUsingForLoop(Dictionary<int, string> dictionaryData, string numberOfItems)
{
var result = string.Empty;

for (int i = 0; i < dictionaryData.Count; i++)
{
var item = dictionaryData.ElementAt(i);
result += item.Value;
}

return result;
}

[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public string WhenDictionaryParallelEnumerable(Dictionary<int, string> dictionaryData, string numberOfItems)
{
using var partialResults = new ThreadLocal<string>(() => string.Empty, trackAllValues: true);

dictionaryData.AsParallel().ForAll(testValue => partialResults.Value += testValue.Value);

return string.Concat(partialResults.Values);
}
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using BenchmarkDotNet.Engines;

namespace Benchmark
{
public class DictionaryIterateBenchmark
{
private Dictionary<int, string> FillData(int count)
{
var testValues = new Dictionary<int, string>();
namespace Benchmark;

for (int i = 0; i < count; i++)
{
testValues.Add(i, "value-" + i);
}
public class DictionaryIterateBenchmark
{
private readonly Consumer _consumer = new();

return testValues;
}
private Dictionary<int, string> FillData(int count)
{
var testValues = new Dictionary<int, string>();

public IEnumerable<object[]> SampleData()
for (int i = 0; i < count; i++)
{
yield return new object[] { FillData(100), "100" };
yield return new object[] { FillData(1000), "1000" };
yield return new object[] { FillData(10000), "10000" };
testValues.Add(i, "value-" + i);
}

[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public void WhenDictionaryUsingForEach(Dictionary<int, string> dictionaryData, string numberOfItems)
{
foreach (var testValue in dictionaryData)
{
var result = testValue.Value;
}
}
return testValues;
}

[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public void WhenDictionaryUsingForLoop(Dictionary<int, string> dictionaryData, string numberOfItems)
public IEnumerable<object[]> SampleData()
{
yield return new object[] { FillData(100), "100" };
yield return new object[] { FillData(1000), "1000" };
yield return new object[] { FillData(10000), "10000" };
}

[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public void WhenDictionaryUsingForEach(Dictionary<int, string> dictionaryData, string numberOfItems)
{
foreach (var testValue in dictionaryData)
{
for (int i = 0; i < dictionaryData.Count; i++)
{
var item = dictionaryData.ElementAt(i);
var result = item.Value;
}
_consumer.Consume(testValue.Value);
}
}

[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public void WhenDictionaryParallelEnumerable(Dictionary<int, string> dictionaryData, string numberOfItems)
[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public void WhenDictionaryUsingForLoop(Dictionary<int, string> dictionaryData, string numberOfItems)
{
for (int i = 0; i < dictionaryData.Count; i++)
{
var result = string.Empty;

dictionaryData.AsParallel().ForAll(testValue => result = testValue.Value);
var item = dictionaryData.ElementAt(i);
_consumer.Consume(item.Value);
}
}

[Benchmark]
[ArgumentsSource(nameof(SampleData))]
public void WhenDictionaryParallelEnumerable(Dictionary<int, string> dictionaryData, string numberOfItems)
{
dictionaryData.AsParallel().ForAll(testValue => _consumer.Consume(testValue.Value));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using BenchmarkDotNet.Running;

namespace Benchmark
namespace Benchmark;

public class ProgramBenchmark
{
public class ProgramBenchmark
static void Main(string[] args)
{
static void Main(string[] args)
{
BenchmarkRunner.Run<DictionaryIterateBenchmark>();
}
BenchmarkRunner.Run(typeof(ProgramBenchmark).Assembly, args: args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,87 @@
namespace IterateThroughDictionary
namespace IterateThroughDictionary;

public class Program
{
public class Program
public static void Main(string[] args)
{
public static void Main(string[] args)
var monthsInYear = new Dictionary<int, string>
{
var monthsInYear = new Dictionary<int, string>
{
{1, "January" },
{2, "February" },
{3, "March" },
{4, "April" }
};

SubDictionaryUsingForEach(monthsInYear);
SubDictionaryKeyValuePair(monthsInYear);
SubDictionaryForLoop(monthsInYear);
SubDictionaryParallelEnumerable(monthsInYear);
{1, "January" },
{2, "February" },
{3, "March" },
{4, "April" }
};

SubDictionaryUsingForEach(monthsInYear);
SubDictionaryKeyValuePair(monthsInYear);
SubDictionaryUsingKeys(monthsInYear);
SubDictionaryUsingValues(monthsInYear);
SubDictionaryForLoop(monthsInYear);
SubDictionaryParallelEnumerable(monthsInYear);
SubDictionaryRemoveWhileIterating(monthsInYear);
}

public static void SubDictionaryUsingForEach(Dictionary<int, string> monthsInYear)
{
foreach (var month in monthsInYear)
{
Console.WriteLine($"{month.Key} : {month.Value}");
}
}

public static void SubDictionaryUsingForEach(Dictionary<int, string> monthsInYear)
public static void SubDictionaryKeyValuePair(Dictionary<int, string> monthsInYear)
{
foreach (KeyValuePair<int, string> entry in monthsInYear)
{
foreach (var month in monthsInYear)
{
Console.WriteLine($"{month.Key } : {month.Value}");
}
Console.WriteLine($"{entry.Key} : {entry.Value}");
}

public static void SubDictionaryKeyValuePair(Dictionary<int, string> monthsInYear)
foreach (var (key, value) in monthsInYear)
{
foreach (KeyValuePair<int, string> entry in monthsInYear)
{
Console.WriteLine($"{entry.Key} : {entry.Value}");
}
Console.WriteLine($"{key} : {value}");
}
}

foreach (var (key, value) in monthsInYear)
{
Console.WriteLine($"{key} : {value}");
}
public static void SubDictionaryUsingKeys(Dictionary<int, string> monthsInYear)
{
foreach (var monthNumber in monthsInYear.Keys)
{
Console.WriteLine(monthNumber);
}
}

public static void SubDictionaryForLoop(Dictionary<int, string> monthsInYear)
public static void SubDictionaryUsingValues(Dictionary<int, string> monthsInYear)
{
foreach (var monthName in monthsInYear.Values)
{
for (int index = 0; index < monthsInYear.Count; index++)
{
KeyValuePair<int, string> month = monthsInYear.ElementAt(index);
Console.WriteLine(monthName);
}
}

Console.WriteLine($"{month.Key} : {month.Value}");
public static void SubDictionaryRemoveWhileIterating(Dictionary<int, string> monthsInYear)
{
foreach (var (monthNumber, monthName) in monthsInYear)
{
if (monthName.StartsWith('J'))
{
monthsInYear.Remove(monthNumber);
}
}
}

public static void SubDictionaryParallelEnumerable(Dictionary<int, string> monthsInYear)
public static void SubDictionaryForLoop(Dictionary<int, string> monthsInYear)
{
for (int index = 0; index < monthsInYear.Count; index++)
{
monthsInYear.AsParallel()
.ForAll(month => Console.WriteLine($"{month.Key} : {month.Value}"));
KeyValuePair<int, string> month = monthsInYear.ElementAt(index);

Console.WriteLine($"{month.Key} : {month.Value}");
}
}

public static void SubDictionaryParallelEnumerable(Dictionary<int, string> monthsInYear)
{
monthsInYear.AsParallel()
.ForAll(month => Console.WriteLine($"{month.Key} : {month.Value}"));
}
}
Loading
Loading