Created
March 31, 2019 18:20
-
-
Save meziantou/323e316f7a3b195a637980ed74600900 to your computer and use it in GitHub Desktop.
Collection performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class Program | |
{ | |
private static void Main() | |
{ | |
BenchmarkRunner.Run<Remove>(); | |
} | |
} | |
[CoreJob] | |
[MemoryDiagnoser] | |
public class AddFirst | |
{ | |
[Params(100_000)] | |
public int Size { get; set; } | |
[Benchmark] | |
public void LinkedList() | |
{ | |
var collection = new LinkedList<int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.AddFirst(i); | |
} | |
} | |
[Benchmark] | |
public void List() | |
{ | |
var collection = new List<int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Insert(0, i); | |
} | |
} | |
} | |
[CoreJob] | |
[MemoryDiagnoser] | |
public class Add | |
{ | |
[Params(100_000)] | |
public int Size { get; set; } | |
[Benchmark] | |
public void LinkedList() | |
{ | |
var collection = new LinkedList<int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.AddLast(i); | |
} | |
} | |
[Benchmark] | |
public void List() | |
{ | |
var collection = new List<int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Add(i); | |
} | |
} | |
[Benchmark] | |
public void SortedList() | |
{ | |
var collection = new SortedList<int, int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Add(i, i); | |
} | |
} | |
[Benchmark] | |
public void HashSet() | |
{ | |
var collection = new HashSet<int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Add(i); | |
} | |
} | |
[Benchmark] | |
public void SortedSet() | |
{ | |
var collection = new SortedSet<int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Add(i); | |
} | |
} | |
[Benchmark] | |
public void Dictionary() | |
{ | |
var collection = new Dictionary<int, int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Add(i, i); | |
} | |
} | |
[Benchmark] | |
public void SortedDictionary() | |
{ | |
var collection = new SortedDictionary<int, int>(); | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Add(i, i); | |
} | |
} | |
} | |
[CoreJob] | |
[MemoryDiagnoser] | |
public class Contains | |
{ | |
[Params(10_000)] | |
public int Size { get; set; } | |
private LinkedList<int> _linkedList; | |
private List<int> _list; | |
private SortedList<int, int> _sortedList; | |
private HashSet<int> _hashSet; | |
private SortedSet<int> _sortedSet; | |
private Dictionary<int, int> _dictionary; | |
private SortedDictionary<int, int> _sortedDictionary; | |
[GlobalSetup] | |
public void GlobalSetup() | |
{ | |
_linkedList = new LinkedList<int>(Initialize()); | |
_list = new List<int>(Initialize()); | |
_sortedList = new SortedList<int, int>(InitializeDictionary()); | |
_hashSet = new HashSet<int>(Initialize()); | |
_sortedSet = new SortedSet<int>(Initialize()); | |
_dictionary = new Dictionary<int, int>(InitializeDictionary()); | |
_sortedDictionary = new SortedDictionary<int, int>(InitializeDictionary()); | |
} | |
private IEnumerable<int> Initialize() | |
{ | |
for (int i = 0; i < Size; i++) | |
{ | |
yield return i; | |
} | |
} | |
private IDictionary<int, int> InitializeDictionary() | |
{ | |
var dict = new Dictionary<int, int>(); | |
for (int i = 0; i < Size; i++) | |
{ | |
dict.Add(i, i); | |
} | |
return dict; | |
} | |
[Benchmark] | |
public void LinkedList() | |
{ | |
var collection = _linkedList; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Contains(i); | |
} | |
} | |
[Benchmark] | |
public void List() | |
{ | |
var collection = _list; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Contains(i); | |
} | |
} | |
[Benchmark] | |
public void SortedList() | |
{ | |
var collection = _sortedList; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.ContainsKey(i); | |
} | |
} | |
[Benchmark] | |
public void HashSet() | |
{ | |
var collection = _hashSet; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Contains(i); | |
} | |
} | |
[Benchmark] | |
public void SortedSet() | |
{ | |
var collection = _sortedSet; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Contains(i); | |
} | |
} | |
[Benchmark] | |
public void Dictionary() | |
{ | |
var collection = _dictionary; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.ContainsKey(i); | |
} | |
} | |
[Benchmark] | |
public void SortedDictionary() | |
{ | |
var collection = _sortedDictionary; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.ContainsKey(i); | |
} | |
} | |
} | |
[CoreJob] | |
[MemoryDiagnoser] | |
public class Remove | |
{ | |
[Params(10_000)] | |
public int Size { get; set; } | |
private LinkedList<int> _linkedList; | |
private List<int> _list; | |
private SortedList<int, int> _sortedList; | |
private HashSet<int> _hashSet; | |
private SortedSet<int> _sortedSet; | |
private Dictionary<int, int> _dictionary; | |
private SortedDictionary<int, int> _sortedDictionary; | |
[IterationSetup] | |
public void IterationSetup() | |
{ | |
_linkedList = new LinkedList<int>(Initialize()); | |
_list = new List<int>(Initialize()); | |
_sortedList = new SortedList<int, int>(InitializeDictionary()); | |
_hashSet = new HashSet<int>(Initialize()); | |
_sortedSet = new SortedSet<int>(Initialize()); | |
_dictionary = new Dictionary<int, int>(InitializeDictionary()); | |
_sortedDictionary = new SortedDictionary<int, int>(InitializeDictionary()); | |
} | |
private IEnumerable<int> Initialize() | |
{ | |
for (int i = 0; i < Size; i++) | |
{ | |
yield return i; | |
} | |
} | |
private IDictionary<int, int> InitializeDictionary() | |
{ | |
var dict = new Dictionary<int, int>(); | |
for (int i = 0; i < Size; i++) | |
{ | |
dict.Add(i, i); | |
} | |
return dict; | |
} | |
[Benchmark] | |
public void LinkedList() | |
{ | |
var collection = _linkedList; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.RemoveFirst(); | |
} | |
} | |
[Benchmark] | |
public void List() | |
{ | |
var collection = _list; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.RemoveAt(0); | |
} | |
} | |
[Benchmark] | |
public void SortedList() | |
{ | |
var collection = _sortedList; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.RemoveAt(0); | |
} | |
} | |
[Benchmark] | |
public void HashSet() | |
{ | |
var collection = _hashSet; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Remove(i); | |
} | |
} | |
[Benchmark] | |
public void SortedSet() | |
{ | |
var collection = _sortedSet; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Remove(i); | |
} | |
} | |
[Benchmark] | |
public void Dictionary() | |
{ | |
var collection = _dictionary; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Remove(i); | |
} | |
} | |
[Benchmark] | |
public void SortedDictionary() | |
{ | |
var collection = _sortedDictionary; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.Remove(i); | |
} | |
} | |
} | |
[CoreJob] | |
[MemoryDiagnoser] | |
public class RemoveLast | |
{ | |
[Params(10_000)] | |
public int Size { get; set; } | |
private LinkedList<int> _linkedList; | |
private List<int> _list; | |
private SortedList<int, int> _sortedList; | |
private HashSet<int> _hashSet; | |
private SortedSet<int> _sortedSet; | |
private Dictionary<int, int> _dictionary; | |
private SortedDictionary<int, int> _sortedDictionary; | |
[IterationSetup] | |
public void IterationSetup() | |
{ | |
_linkedList = new LinkedList<int>(Initialize()); | |
_list = new List<int>(Initialize()); | |
_sortedList = new SortedList<int, int>(InitializeDictionary()); | |
_hashSet = new HashSet<int>(Initialize()); | |
_sortedSet = new SortedSet<int>(Initialize()); | |
_dictionary = new Dictionary<int, int>(InitializeDictionary()); | |
_sortedDictionary = new SortedDictionary<int, int>(InitializeDictionary()); | |
} | |
private IEnumerable<int> Initialize() | |
{ | |
for (int i = 0; i < Size; i++) | |
{ | |
yield return i; | |
} | |
} | |
private IDictionary<int, int> InitializeDictionary() | |
{ | |
var dict = new Dictionary<int, int>(); | |
for (int i = 0; i < Size; i++) | |
{ | |
dict.Add(i, i); | |
} | |
return dict; | |
} | |
[Benchmark] | |
public void LinkedList() | |
{ | |
var collection = _linkedList; | |
var size = Size; | |
for (int i = 0; i < size; i++) | |
{ | |
collection.RemoveLast(); | |
} | |
} | |
[Benchmark] | |
public void List() | |
{ | |
var collection = _list; | |
var size = Size; | |
for (int i = size - 1; i >= 0; i--) | |
{ | |
collection.RemoveAt(i); | |
} | |
} | |
[Benchmark] | |
public void SortedList() | |
{ | |
var collection = _sortedList; | |
var size = Size; | |
for (int i = size - 1; i >= 0; i--) | |
{ | |
collection.RemoveAt(i); | |
} | |
} | |
[Benchmark] | |
public void HashSet() | |
{ | |
var collection = _hashSet; | |
var size = Size; | |
for (int i = size - 1; i >= 0; i--) | |
{ | |
collection.Remove(i); | |
} | |
} | |
[Benchmark] | |
public void SortedSet() | |
{ | |
var collection = _sortedSet; | |
var size = Size; | |
for (int i = size - 1; i >= 0; i--) | |
{ | |
collection.Remove(i); | |
} | |
} | |
[Benchmark] | |
public void Dictionary() | |
{ | |
var collection = _dictionary; | |
var size = Size; | |
for (int i = size - 1; i >= 0; i--) | |
{ | |
collection.Remove(i); | |
} | |
} | |
[Benchmark] | |
public void SortedDictionary() | |
{ | |
var collection = _sortedDictionary; | |
var size = Size; | |
for (int i = size - 1; i >= 0; i--) | |
{ | |
collection.Remove(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment