Answer the question
In order to leave comments, you need to log in
Measuring lookup time in List and SortedDictionary?
Hello, please explain why there is such a difference in the search time in the list and in the dictionary. What have I done wrong? Data in the picture with 5000 elements in the collection
Answer the question
In order to leave comments, you need to log in
Ran the test through BenchmarkDotNet:
public class TestClass
{
public static List<int> list = new(Enumerable.Range(0, 1_000_000));
public static Dictionary<int, int> dict = Enumerable.Range(0, 1_000_000).ToDictionary(x => x, x => x);
public static SortedDictionary<int, int> sorted = new(Enumerable.Range(0, 1_000_000).ToDictionary(x => x, x => x));
[Benchmark]
public void List_First()
{
list.Contains(0);
}
[Benchmark]
public void List_Middle()
{
list.Contains(500_000);
}
[Benchmark]
public void List_Last()
{
list.Contains(1_000_000);
}
[Benchmark]
public void List_NoMatch()
{
list.Contains(-1);
}
[Benchmark]
public void Dictionary_First()
{
dict.ContainsKey(0);
}
[Benchmark]
public void Dictionary_Middle()
{
dict.ContainsKey(500_000);
}
[Benchmark]
public void Dictionary_Last()
{
dict.ContainsKey(1_000_000);
}
[Benchmark]
public void Dictionary_NoMatch()
{
dict.ContainsKey(-1);
}
[Benchmark]
public void Sorted_First()
{
sorted.ContainsKey(0);
}
[Benchmark]
public void Sorted_Middle()
{
sorted.ContainsKey(500_000);
}
[Benchmark]
public void Sorted_Last()
{
sorted.ContainsKey(1_000_000);
}
[Benchmark]
public void Sorted_NoMatch()
{
sorted.ContainsKey(-1);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question