Skip to content

Instantly share code, notes, and snippets.

@xtqqczze
Last active August 26, 2025 00:33
Show Gist options
  • Save xtqqczze/5209e139d4bea52188a6a27b34212def to your computer and use it in GitHub Desktop.
Save xtqqczze/5209e139d4bea52188a6a27b34212def to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Linq;
using BenchmarkDotNet.Attributes;
[MemoryDiagnoser]
public class Benchmarks
{
private readonly IDictionary _dict = Enumerable.Range(0, 10000).ToDictionary(i => i.ToString(), i => string.Empty);
[Benchmark]
public void DictionaryEntry()
{
foreach (DictionaryEntry entry in _dict)
{
_ = (string)entry.Value;
}
}
[Benchmark]
public void IDictionaryEnumerator()
{
IDictionaryEnumerator e = _dict.GetEnumerator();
try
{
while (e.MoveNext())
{
DictionaryEntry entry = e.Entry;
_ = (string)entry.Value;
}
}
finally
{
(e as IDisposable)?.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment