From 13e43b2ebd0d4d8d1ead73ef6dc6a5cd7415db34 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 20:29:32 +0200 Subject: [PATCH 01/31] ctor default size all collections benchmarks --- src/benchmarks/Categories.cs | 1 + .../System.Collections/CtorDefaultSize.cs | 80 +++++++++++++++++++ .../CtorDefaultSizeNonGeneric.cs | 22 +++++ 3 files changed, 103 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs create mode 100644 src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs diff --git a/src/benchmarks/Categories.cs b/src/benchmarks/Categories.cs index 61e8d802bd8..a5171a224ec 100644 --- a/src/benchmarks/Categories.cs +++ b/src/benchmarks/Categories.cs @@ -16,5 +16,6 @@ public static class Categories public const string LINQ = "LINQ"; public const string SIMD = "SIMD"; public const string Span = "Span"; + public const string Collections = "Collections"; } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs b/src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs new file mode 100644 index 00000000000..b8e1525dba9 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs @@ -0,0 +1,80 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Collections.Immutable; +using BenchmarkDotNet.Attributes; +using Benchmarks; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections)] + [GenericTypeArguments(typeof(int), typeof(int))] // value type + [GenericTypeArguments(typeof(string), typeof(string))] // reference type + public class CtorDefaultSize + { + [Benchmark] + public TKey[] Array() => new TKey[4]; // array has no default size, List has = 4 so I decided to use 4 here + + [Benchmark] + public List List() => new List(); + + [Benchmark] + public LinkedList LinkedList() => new LinkedList(); + + [Benchmark] + public HashSet HashSet() => new HashSet(); + + [Benchmark] + public Dictionary Dictionary() => new Dictionary(); + + [Benchmark] + public Queue Queue() => new Queue(); + + [Benchmark] + public Stack Stack() => new Stack(); + + [Benchmark] + public SortedList SortedList() => new SortedList(); + + [Benchmark] + public SortedSet SortedSet() => new SortedSet(); + + [Benchmark] + public SortedDictionary SortedDictionary() => new SortedDictionary(); + + [Benchmark] + public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(); + + [Benchmark] + public ConcurrentQueue ConcurrentQueue() => new ConcurrentQueue(); + + [Benchmark] + public ConcurrentStack ConcurrentStack() => new ConcurrentStack(); + + [Benchmark] + public ConcurrentBag ConcurrentBag() => new ConcurrentBag(); + + [Benchmark] + public ImmutableArray ImmutableArray() => Immutable.ImmutableArray.Create(); + + [Benchmark] + public ImmutableDictionary ImmutableDictionary() => Immutable.ImmutableDictionary.Create(); + + [Benchmark] + public ImmutableHashSet ImmutableHashSet() => Immutable.ImmutableHashSet.Create(); + + [Benchmark] + public ImmutableList ImmutableList() => Immutable.ImmutableList.Create(); + + [Benchmark] + public ImmutableQueue ImmutableQueue() => Immutable.ImmutableQueue.Create(); + + [Benchmark] + public ImmutableStack ImmutableStack() => Immutable.ImmutableStack.Create(); + + [Benchmark] + public ImmutableSortedDictionary ImmutableSortedDictionary() => Immutable.ImmutableSortedDictionary.Create(); + + [Benchmark] + public ImmutableSortedSet ImmutableSortedSet() => Immutable.ImmutableSortedSet.Create(); + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs b/src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs new file mode 100644 index 00000000000..a32637c8b57 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs @@ -0,0 +1,22 @@ +using BenchmarkDotNet.Attributes; + +namespace System.Collections +{ + public class CtorDefaultSizeNonGeneric + { + [Benchmark] + public ArrayList ArrayList() => new ArrayList(); + + [Benchmark] + public Hashtable Hashtable() => new Hashtable(); + + [Benchmark] + public Queue Queue() => new Queue(); + + [Benchmark] + public Stack Stack() => new Stack(); + + [Benchmark] + public SortedList SortedList() => new SortedList(); + } +} \ No newline at end of file From 85f98c1dcd55aacc685c22ee18a80dd28f8455df Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 21:00:24 +0200 Subject: [PATCH 02/31] ctor given size all collections benchmarks --- .../System.Collections/CtorGivenSize.cs | 43 +++++++++++++++++++ .../CtorGivenSizeNonGeneric.cs | 25 +++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/CtorGivenSize.cs create mode 100644 src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs diff --git a/src/benchmarks/corefx/System.Collections/CtorGivenSize.cs b/src/benchmarks/corefx/System.Collections/CtorGivenSize.cs new file mode 100644 index 00000000000..44d77eee23d --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/CtorGivenSize.cs @@ -0,0 +1,43 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using BenchmarkDotNet.Attributes; +using Benchmarks; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections)] + [GenericTypeArguments(typeof(int), typeof(int))] // value type + [GenericTypeArguments(typeof(string), typeof(string))] // reference type + public class CtorGiventSize + { + private const int ConcurrencyLevel = 4; + + [Params(100)] + public int Size; + + [Benchmark] + public TKey[] Array() => new TKey[Size]; + + [Benchmark] + public List List() => new List(Size); + +#if !NET461 // API added in .NET Core 2.0 + [Benchmark] + public HashSet HashSet() => new HashSet(Size); +#endif + [Benchmark] + public Dictionary Dictionary() => new Dictionary(Size); + + [Benchmark] + public Queue Queue() => new Queue(Size); + + [Benchmark] + public Stack Stack() => new Stack(Size); + + [Benchmark] + public SortedList SortedList() => new SortedList(Size); + + [Benchmark] + public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(ConcurrencyLevel, Size); + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs b/src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs new file mode 100644 index 00000000000..5c48d41c410 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs @@ -0,0 +1,25 @@ +using BenchmarkDotNet.Attributes; + +namespace System.Collections +{ + public class CtorGivenSizeNonGeneric + { + [Params(100)] + public int Size; + + [Benchmark] + public ArrayList ArrayList() => new ArrayList(Size); + + [Benchmark] + public Hashtable Hashtable() => new Hashtable(Size); + + [Benchmark] + public Queue Queue() => new Queue(Size); + + [Benchmark] + public Stack Stack() => new Stack(Size); + + [Benchmark] + public SortedList SortedList() => new SortedList(Size); + } +} \ No newline at end of file From 34fc8042620567667a40f33a25ac69bb57b28ed3 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 21:25:03 +0200 Subject: [PATCH 03/31] ctor from collection all collections benchmarks --- src/benchmarks/Categories.cs | 2 + .../corefx/Common/UniqueValuesGenerator.cs | 55 +++++++++++ .../System.Collections/CtorDefaultSize.cs | 2 +- .../CtorDefaultSizeNonGeneric.cs | 2 + .../System.Collections/CtorFromCollection.cs | 91 +++++++++++++++++++ .../CtorFromCollectionNonGeneric.cs | 38 ++++++++ .../System.Collections/CtorGivenSize.cs | 2 +- .../CtorGivenSizeNonGeneric.cs | 2 + 8 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 src/benchmarks/corefx/Common/UniqueValuesGenerator.cs create mode 100644 src/benchmarks/corefx/System.Collections/CtorFromCollection.cs create mode 100644 src/benchmarks/corefx/System.Collections/CtorFromCollectionNonGeneric.cs diff --git a/src/benchmarks/Categories.cs b/src/benchmarks/Categories.cs index a5171a224ec..870cf433321 100644 --- a/src/benchmarks/Categories.cs +++ b/src/benchmarks/Categories.cs @@ -17,5 +17,7 @@ public static class Categories public const string SIMD = "SIMD"; public const string Span = "Span"; public const string Collections = "Collections"; + public const string GenericCollections = "GenericCollections"; + public const string NonGenericCollections = "NonGenericCollections"; } } \ No newline at end of file diff --git a/src/benchmarks/corefx/Common/UniqueValuesGenerator.cs b/src/benchmarks/corefx/Common/UniqueValuesGenerator.cs new file mode 100644 index 00000000000..0ce2dcb6952 --- /dev/null +++ b/src/benchmarks/corefx/Common/UniqueValuesGenerator.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Helpers +{ + internal static class UniqueValuesGenerator + { + internal static T[] GenerateArray(int count) + { + var random = new Random(12345); // we always use the same seed to have repeatable results! + + var uniqueValues = new HashSet(); + + while (uniqueValues.Count != count) + { + T value = GenerateValue(random); + + if (!uniqueValues.Contains(value)) + uniqueValues.Add(value); + } + + return uniqueValues.ToArray(); + } + + internal static Dictionary GenerateDictionary(int count) + { + var random = new Random(12345); // we always use the same seed to have repeatable results! + + var dictionary = new Dictionary(); + + while (dictionary.Count != count) + { + TKey key = GenerateValue(random); + + if (!dictionary.ContainsKey(key)) + dictionary.Add(key, GenerateValue(random)); + } + + return dictionary; + } + + private static T GenerateValue(Random random) + { + if (typeof(T) == typeof(int)) + return (T)(object)random.Next(); + if (typeof(T) == typeof(double)) + return (T)(object)random.NextDouble(); + if (typeof(T) == typeof(string)) + return (T)(object)Guid.NewGuid().ToString(); // I am open to better ideas! + + throw new NotImplementedException($"{typeof(T).Name} is not implemented"); + } + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs b/src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs index b8e1525dba9..65ac4f4d362 100644 --- a/src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs @@ -6,7 +6,7 @@ namespace System.Collections { - [BenchmarkCategory(Categories.CoreFX, Categories.Collections)] + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] [GenericTypeArguments(typeof(int), typeof(int))] // value type [GenericTypeArguments(typeof(string), typeof(string))] // reference type public class CtorDefaultSize diff --git a/src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs b/src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs index a32637c8b57..2d01d66c25e 100644 --- a/src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs +++ b/src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs @@ -1,7 +1,9 @@ using BenchmarkDotNet.Attributes; +using Benchmarks; namespace System.Collections { + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.NonGenericCollections)] public class CtorDefaultSizeNonGeneric { [Benchmark] diff --git a/src/benchmarks/corefx/System.Collections/CtorFromCollection.cs b/src/benchmarks/corefx/System.Collections/CtorFromCollection.cs new file mode 100644 index 00000000000..58f18d36709 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/CtorFromCollection.cs @@ -0,0 +1,91 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Collections.Immutable; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int), typeof(int))] // value type + [GenericTypeArguments(typeof(string), typeof(string))] // reference type + public class CtorFromCollection + { + private ICollection _collection; + private IDictionary _dictionary; + + [Params(100)] + public int Size; + + [GlobalSetup] + public void Setup() + { + _collection = UniqueValuesGenerator.GenerateArray(Size); + _dictionary = UniqueValuesGenerator.GenerateDictionary(Size); + } + + [Benchmark] + public List List() => new List(_collection); + + [Benchmark] + public LinkedList LinkedList() => new LinkedList(_collection); + + [Benchmark] + public HashSet HashSet() => new HashSet(_collection); + + [Benchmark] + public Dictionary Dictionary() => new Dictionary(_dictionary); + + [Benchmark] + public Queue Queue() => new Queue(_collection); + + [Benchmark] + public Stack Stack() => new Stack(_collection); + + [Benchmark] + public SortedList SortedList() => new SortedList(_dictionary); + + [Benchmark] + public SortedSet SortedSet() => new SortedSet(_collection); + + [Benchmark] + public SortedDictionary SortedDictionary() => new SortedDictionary(_dictionary); + + [Benchmark] + public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(_dictionary); + + [Benchmark] + public ConcurrentQueue ConcurrentQueue() => new ConcurrentQueue(_collection); + + [Benchmark] + public ConcurrentStack ConcurrentStack() => new ConcurrentStack(_collection); + + [Benchmark] + public ConcurrentBag ConcurrentBag() => new ConcurrentBag(_collection); + + [Benchmark] + public ImmutableArray ImmutableArray() => Immutable.ImmutableArray.CreateRange(_collection); + + [Benchmark] + public ImmutableDictionary ImmutableDictionary() => Immutable.ImmutableDictionary.CreateRange(_dictionary); + + [Benchmark] + public ImmutableHashSet ImmutableHashSet() => Immutable.ImmutableHashSet.CreateRange(_collection); + + [Benchmark] + public ImmutableList ImmutableList() => Immutable.ImmutableList.CreateRange(_collection); + + [Benchmark] + public ImmutableQueue ImmutableQueue() => Immutable.ImmutableQueue.CreateRange(_collection); + + [Benchmark] + public ImmutableStack ImmutableStack() => Immutable.ImmutableStack.CreateRange(_collection); + + [Benchmark] + public ImmutableSortedDictionary ImmutableSortedDictionary() => Immutable.ImmutableSortedDictionary.CreateRange(_dictionary); + + [Benchmark] + public ImmutableSortedSet ImmutableSortedSet() => Immutable.ImmutableSortedSet.CreateRange(_collection); + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/CtorFromCollectionNonGeneric.cs b/src/benchmarks/corefx/System.Collections/CtorFromCollectionNonGeneric.cs new file mode 100644 index 00000000000..ce6ad18f13f --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/CtorFromCollectionNonGeneric.cs @@ -0,0 +1,38 @@ +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + public class CtorFromCollectionNonGeneric + { + private ICollection _collection; + private IDictionary _dictionary; + + [Params(100)] + public int Size; + + [GlobalSetup] + public void Setup() + { + _collection = UniqueValuesGenerator.GenerateArray(Size); + _dictionary = UniqueValuesGenerator.GenerateDictionary(Size); + } + + [Benchmark] + public ArrayList ArrayList() => new ArrayList(_collection); + + [Benchmark] + public Hashtable Hashtable() => new Hashtable(_dictionary); + + [Benchmark] + public Queue Queue() => new Queue(_collection); + + [Benchmark] + public Stack Stack() => new Stack(_collection); + + [Benchmark] + public SortedList SortedList() => new SortedList(_dictionary); + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/CtorGivenSize.cs b/src/benchmarks/corefx/System.Collections/CtorGivenSize.cs index 44d77eee23d..e03afaa66c4 100644 --- a/src/benchmarks/corefx/System.Collections/CtorGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/CtorGivenSize.cs @@ -5,7 +5,7 @@ namespace System.Collections { - [BenchmarkCategory(Categories.CoreFX, Categories.Collections)] + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] [GenericTypeArguments(typeof(int), typeof(int))] // value type [GenericTypeArguments(typeof(string), typeof(string))] // reference type public class CtorGiventSize diff --git a/src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs b/src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs index 5c48d41c410..f07fda2e85f 100644 --- a/src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs +++ b/src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs @@ -1,7 +1,9 @@ using BenchmarkDotNet.Attributes; +using Benchmarks; namespace System.Collections { + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] public class CtorGivenSizeNonGeneric { [Params(100)] From af04aa77f63177469cd0be06d47dd551ccc05d45 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 21:39:27 +0200 Subject: [PATCH 04/31] move to a dedicated folder, add categories --- .../corefx/System.Collections/{ => Create}/CtorDefaultSize.cs | 0 .../System.Collections/{ => Create}/CtorDefaultSizeNonGeneric.cs | 0 .../corefx/System.Collections/{ => Create}/CtorFromCollection.cs | 0 .../{ => Create}/CtorFromCollectionNonGeneric.cs | 0 .../corefx/System.Collections/{ => Create}/CtorGivenSize.cs | 0 .../System.Collections/{ => Create}/CtorGivenSizeNonGeneric.cs | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename src/benchmarks/corefx/System.Collections/{ => Create}/CtorDefaultSize.cs (100%) rename src/benchmarks/corefx/System.Collections/{ => Create}/CtorDefaultSizeNonGeneric.cs (100%) rename src/benchmarks/corefx/System.Collections/{ => Create}/CtorFromCollection.cs (100%) rename src/benchmarks/corefx/System.Collections/{ => Create}/CtorFromCollectionNonGeneric.cs (100%) rename src/benchmarks/corefx/System.Collections/{ => Create}/CtorGivenSize.cs (100%) rename src/benchmarks/corefx/System.Collections/{ => Create}/CtorGivenSizeNonGeneric.cs (100%) diff --git a/src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs similarity index 100% rename from src/benchmarks/corefx/System.Collections/CtorDefaultSize.cs rename to src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs diff --git a/src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSizeNonGeneric.cs similarity index 100% rename from src/benchmarks/corefx/System.Collections/CtorDefaultSizeNonGeneric.cs rename to src/benchmarks/corefx/System.Collections/Create/CtorDefaultSizeNonGeneric.cs diff --git a/src/benchmarks/corefx/System.Collections/CtorFromCollection.cs b/src/benchmarks/corefx/System.Collections/Create/CtorFromCollection.cs similarity index 100% rename from src/benchmarks/corefx/System.Collections/CtorFromCollection.cs rename to src/benchmarks/corefx/System.Collections/Create/CtorFromCollection.cs diff --git a/src/benchmarks/corefx/System.Collections/CtorFromCollectionNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Create/CtorFromCollectionNonGeneric.cs similarity index 100% rename from src/benchmarks/corefx/System.Collections/CtorFromCollectionNonGeneric.cs rename to src/benchmarks/corefx/System.Collections/Create/CtorFromCollectionNonGeneric.cs diff --git a/src/benchmarks/corefx/System.Collections/CtorGivenSize.cs b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs similarity index 100% rename from src/benchmarks/corefx/System.Collections/CtorGivenSize.cs rename to src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs diff --git a/src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSizeNonGeneric.cs similarity index 100% rename from src/benchmarks/corefx/System.Collections/CtorGivenSizeNonGeneric.cs rename to src/benchmarks/corefx/System.Collections/Create/CtorGivenSizeNonGeneric.cs From 8fb685db8a95bac0b768e1429f1b039e0aa2c22b Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 22:14:04 +0200 Subject: [PATCH 05/31] iterate for each all collections --- .../Iterate/IterateForEach.cs | 327 ++++++++++++++++++ 1 file changed, 327 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs new file mode 100644 index 00000000000..04817172f92 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs @@ -0,0 +1,327 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Collections.Immutable; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int), typeof(int))] // value type + [GenericTypeArguments(typeof(string), typeof(string))] // reference type + public class IterateForEach + { + [Params(100)] + public int Size; + + private TValue[] _array; + private List _list; + private LinkedList _linkedlist; + private HashSet _hashset; + private Dictionary _dictionary; + private Queue _queue; + private Stack _stack; + private SortedList _sortedlist; + private SortedSet _sortedset; + private SortedDictionary _sorteddictionary; + private ConcurrentDictionary _concurrentdictionary; + private ConcurrentQueue _concurrentqueue; + private ConcurrentStack _concurrentstack; + private ConcurrentBag _concurrentbag; + private ImmutableArray _immutablearray; + private ImmutableDictionary _immutabledictionary; + private ImmutableHashSet _immutablehashset; + private ImmutableList _immutablelist; + private ImmutableQueue _immutablequeue; + private ImmutableStack _immutablestack; + private ImmutableSortedDictionary _immutablesorteddictionary; + private ImmutableSortedSet _immutablesortedset; + + [GlobalSetup(Target = nameof(Array))] + public void SetupArray() => _array = UniqueValuesGenerator.GenerateArray(Size); + + [Benchmark] + public TValue Array() + { + TValue result = default; + var local = _array; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(List))] + public void SetupList() => _list = new List(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue List() + { + TValue result = default; + var local = _list; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(LinkedList))] + public void SetupLinkedList() => _linkedlist = new LinkedList(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue LinkedList() + { + TValue result = default; + var local = _linkedlist; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(HashSet))] + public void SetupHashSet() => _hashset = new HashSet(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue HashSet() + { + TValue result = default; + var local = _hashset; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(Dictionary))] + public void SetupDictionary() => _dictionary = new Dictionary(UniqueValuesGenerator.GenerateDictionary(Size)); + + [Benchmark] + public TValue Dictionary() + { + TValue result = default; + var local = _dictionary; + foreach (var item in local) + result = item.Value; + return result; + } + + [GlobalSetup(Target = nameof(Queue))] + public void SetupQueue() => _queue = new Queue(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue Queue() + { + TValue result = default; + var local = _queue; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(Stack))] + public void SetupStack() => _stack = new Stack(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue Stack() + { + TValue result = default; + var local = _stack; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(SortedList))] + public void SetupSortedList() => _sortedlist = new SortedList(UniqueValuesGenerator.GenerateDictionary(Size)); + + [Benchmark] + public TValue SortedList() + { + TValue result = default; + var local = _sortedlist; + foreach (var item in local) + result = item.Value; + return result; + } + + [GlobalSetup(Target = nameof(SortedSet))] + public void SetupSortedSet() => _sortedset = new SortedSet(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue SortedSet() + { + TValue result = default; + var local = _sortedset; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(SortedDictionary))] + public void SetupSortedDictionary() => _sorteddictionary = new SortedDictionary(UniqueValuesGenerator.GenerateDictionary(Size)); + + [Benchmark] + public TValue SortedDictionary() + { + TValue result = default; + var local = _sorteddictionary; + foreach (var item in local) + result = item.Value; + return result; + } + + [GlobalSetup(Target = nameof(ConcurrentDictionary))] + public void SetupConcurrentDictionary() => _concurrentdictionary = new ConcurrentDictionary(UniqueValuesGenerator.GenerateDictionary(Size)); + + [Benchmark] + public TValue ConcurrentDictionary() + { + TValue result = default; + var local = _concurrentdictionary; + foreach (var item in local) + result = item.Value; + return result; + } + + [GlobalSetup(Target = nameof(ConcurrentQueue))] + public void SetupConcurrentQueue() => _concurrentqueue = new ConcurrentQueue(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ConcurrentQueue() + { + TValue result = default; + var local = _concurrentqueue; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(ConcurrentStack))] + public void SetupConcurrentStack() => _concurrentstack = new ConcurrentStack(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ConcurrentStack() + { + TValue result = default; + var local = _concurrentstack; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(ConcurrentBag))] + public void SetupConcurrentBag() => _concurrentbag = new ConcurrentBag(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ConcurrentBag() + { + TValue result = default; + var local = _concurrentbag; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableArray))] + public void SetupImmutableArray() => _immutablearray = Immutable.ImmutableArray.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableArray() + { + TValue result = default; + var local = _immutablearray; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableDictionary))] + public void SetupImmutableDictionary() => _immutabledictionary = Immutable.ImmutableDictionary.CreateRange(UniqueValuesGenerator.GenerateDictionary(Size)); + + [Benchmark] + public TValue ImmutableDictionary() + { + TValue result = default; + var local = _immutabledictionary; + foreach (var item in local) + result = item.Value; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableHashSet))] + public void SetupImmutableHashSet() => _immutablehashset = Immutable.ImmutableHashSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableHashSet() + { + TValue result = default; + var local = _immutablehashset; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableList))] + public void SetupImmutableList() => _immutablelist = Immutable.ImmutableList.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableList() + { + TValue result = default; + var local = _immutablelist; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableQueue))] + public void SetupImmutableQueue() => _immutablequeue = Immutable.ImmutableQueue.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableQueue() + { + TValue result = default; + var local = _immutablequeue; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableStack))] + public void SetupImmutableStack() => _immutablestack = Immutable.ImmutableStack.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableStack() + { + TValue result = default; + var local = _immutablestack; + foreach (var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableSortedDictionary))] + public void SetupImmutableSortedDictionary() => _immutablesorteddictionary = Immutable.ImmutableSortedDictionary.CreateRange(UniqueValuesGenerator.GenerateDictionary(Size)); + + [Benchmark] + public TValue ImmutableSortedDictionary() + { + TValue result = default; + var local = _immutablesorteddictionary; + foreach (var item in local) + result = item.Value; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableSortedSet))] + public void SetupImmutableSortedSet() => _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableSortedSet() + { + TValue result = default; + var local = _immutablesortedset; + foreach (var item in local) + result = item; + return result; + } + } +} \ No newline at end of file From 3321633d7693590b6558d02c2e26a24fdfbae275 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 22:26:19 +0200 Subject: [PATCH 06/31] iterate for loop all collections --- .../System.Collections/Iterate/IterateFor.cs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs new file mode 100644 index 00000000000..1ce09514b06 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs @@ -0,0 +1,88 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class IterateFor + { + [Params(100)] + public int Size; + + private TValue[] _array; + private List _list; + private ImmutableArray _immutablearray; + private ImmutableList _immutablelist; + private ImmutableSortedSet _immutablesortedset; + + [GlobalSetup(Target = nameof(Array))] + public void SetupArray() => _array = UniqueValuesGenerator.GenerateArray(Size); + + [Benchmark] + public TValue Array() + { + TValue result = default; + var local = _array; + for (int i = 0; i < local.Length; i++) + result = local[i]; + return result; + } + + [GlobalSetup(Target = nameof(List))] + public void SetupList() => _list = new List(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue List() + { + TValue result = default; + var local = _list; + for(int i = 0; i < local.Count; i++) + result = local[i]; + return result;; + } + + [GlobalSetup(Target = nameof(ImmutableArray))] + public void SetupImmutableArray() => _immutablearray = Immutable.ImmutableArray.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableArray() + { + TValue result = default; + var local = _immutablearray; + for(int i = 0; i < local.Length; i++) + result = local[i]; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableList))] + public void SetupImmutableList() => _immutablelist = Immutable.ImmutableList.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableList() + { + TValue result = default; + var local = _immutablelist; + for(int i = 0; i < local.Count; i++) + result = local[i]; + return result; + } + + [GlobalSetup(Target = nameof(ImmutableSortedSet))] + public void SetupImmutableSortedSet() => _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public TValue ImmutableSortedSet() + { + TValue result = default; + var local = _immutablesortedset; + for(int i = 0; i < local.Count; i++) + result = local[i]; + return result; + } + } +} \ No newline at end of file From cf7fa2ff7e18d4e6b1065c3110f4d9b3c0ff934a Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 22:40:23 +0200 Subject: [PATCH 07/31] iterate for each non generic collections --- .../Iterate/IterateForEachNonGeneric.cs | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs new file mode 100644 index 00000000000..f87e4e758cd --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs @@ -0,0 +1,84 @@ +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + public class IterateForEachNonGeneric + { + [Params(100)] + public int Size; + + private ArrayList _arraylist; + private Hashtable _hashtable; + private Queue _queue; + private Stack _stack; + private SortedList _sortedlist; + + [GlobalSetup(Target = nameof(ArrayList))] + public void SetupArrayList() => _arraylist = new ArrayList(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public object ArrayList() + { + object result = default; + var local = _arraylist; + foreach(var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(Hashtable))] + public void SetupHashtable() => _hashtable = new Hashtable(UniqueValuesGenerator.GenerateDictionary(Size)); + + [Benchmark] + public object Hashtable() + { + object result = default; + var local = _hashtable; + foreach(var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(Queue))] + public void SetupQueue() => _queue = new Queue(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public object Queue() + { + object result = default; + var local = _queue; + foreach(var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(Stack))] + public void SetupStack() => _stack = new Stack(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public object Stack() + { + object result = default; + var local = _stack; + foreach(var item in local) + result = item; + return result; + } + + [GlobalSetup(Target = nameof(SortedList))] + public void SetupSortedList() => _sortedlist = new SortedList(UniqueValuesGenerator.GenerateDictionary(Size)); + + [Benchmark] + public object SortedList() + { + object result = default; + var local = _sortedlist; + foreach(var item in local) + result = item; + return result; + } + } +} \ No newline at end of file From 578c54684ecdfff0bbf4cfc4d09ba22243b180c3 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 22:45:34 +0200 Subject: [PATCH 08/31] iterate for loop non generic collections --- .../Iterate/IterateForEachNonGeneric.cs | 2 +- .../Iterate/IterateForNonGeneric.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/benchmarks/corefx/System.Collections/Iterate/IterateForNonGeneric.cs diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs index f87e4e758cd..59e4aca9173 100644 --- a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs @@ -4,7 +4,7 @@ namespace System.Collections { - [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.NonGenericCollections)] public class IterateForEachNonGeneric { [Params(100)] diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateForNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateForNonGeneric.cs new file mode 100644 index 00000000000..09981908940 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateForNonGeneric.cs @@ -0,0 +1,28 @@ +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.NonGenericCollections)] + public class IterateForNonGeneric + { + [Params(100)] + public int Size; + + private ArrayList _arraylist; + + [GlobalSetup(Target = nameof(ArrayList))] + public void SetupArrayList() => _arraylist = new ArrayList(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + public object ArrayList() + { + object result = default; + var local = _arraylist; + for(int i = 0; i < local.Count; i++) + result = local[i]; + return result; + } + } +} \ No newline at end of file From cfafed8629c18265f3b977a30ff20bc6a5293570 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 11 Jul 2018 22:53:44 +0200 Subject: [PATCH 09/31] update to BDN which offers glob filtering so I can filter like a pro ;) --- src/benchmarks/Benchmarks.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/benchmarks/Benchmarks.csproj b/src/benchmarks/Benchmarks.csproj index 5f995a38284..b71e4e6d133 100644 --- a/src/benchmarks/Benchmarks.csproj +++ b/src/benchmarks/Benchmarks.csproj @@ -14,7 +14,7 @@ - + From 1f81328d48a297d008176974b73f2f09244d0c05 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 12 Jul 2018 12:41:14 +0200 Subject: [PATCH 10/31] Contains for all generic collections --- .../Contains/ContainsFalse.cs | 173 ++++++++++++++++++ .../Contains/ContainsTrue.cs | 170 +++++++++++++++++ 2 files changed, 343 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs create mode 100644 src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs new file mode 100644 index 00000000000..8c69325c72f --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs @@ -0,0 +1,173 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class ContainsFalse + { + private T[] _notFound; + + private T[] _array; + private List _list; + private LinkedList _linkedlist; + private HashSet _hashset; + private Queue _queue; + private Stack _stack; + private SortedSet _sortedset; + private ImmutableArray _immutablearray; + private ImmutableHashSet _immutablehashset; + private ImmutableList _immutablelist; + private ImmutableSortedSet _immutablesortedset; + + [Params(100)] + public int Size; + + [GlobalSetup] + public void Setup() + { + var values = UniqueValuesGenerator.GenerateArray(Size * 2); + _notFound = values.Take(Size).ToArray(); + var secondHalf = values.Skip(Size).Take(Size).ToArray(); + + _array = secondHalf; + _list = new List(secondHalf); + _linkedlist = new LinkedList(secondHalf); + _hashset = new HashSet(secondHalf); + _queue = new Queue(secondHalf); + _stack = new Stack(secondHalf); + _sortedset = new SortedSet(secondHalf); + _immutablearray = Immutable.ImmutableArray.CreateRange(secondHalf); + _immutablehashset = Immutable.ImmutableHashSet.CreateRange(secondHalf); + _immutablelist = Immutable.ImmutableList.CreateRange(secondHalf); + _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(secondHalf); + } + + [Benchmark] + public bool Array() + { + bool result = default; + var local = _array; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool List() + { + bool result = default; + var local = _list; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool LinkedList() + { + bool result = default; + var local = _linkedlist; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool HashSet() + { + bool result = default; + var local = _hashset; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool Queue() + { + bool result = default; + var local = _queue; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool Stack() + { + bool result = default; + var local = _stack; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool SortedSet() + { + bool result = default; + var local = _sortedset; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool ImmutableArray() + { + bool result = default; + var local = _immutablearray; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool ImmutableHashSet() + { + bool result = default; + var local = _immutablehashset; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool ImmutableList() + { + bool result = default; + var local = _immutablelist; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + + [Benchmark] + public bool ImmutableSortedSet() + { + bool result = default; + var local = _immutablesortedset; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = local.Contains(notFound[i]); + return result; + } + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs new file mode 100644 index 00000000000..90d4c10c4cf --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs @@ -0,0 +1,170 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class ContainsTrue + { + private T[] _values; + + private T[] _array; + private List _list; + private LinkedList _linkedlist; + private HashSet _hashset; + private Queue _queue; + private Stack _stack; + private SortedSet _sortedset; + private ImmutableArray _immutablearray; + private ImmutableHashSet _immutablehashset; + private ImmutableList _immutablelist; + private ImmutableSortedSet _immutablesortedset; + + [Params(100)] + public int Size; + + [GlobalSetup] + public void Setup() + { + _values = UniqueValuesGenerator.GenerateArray(Size); + _array = _values.ToArray(); + _list = new List(_values); + _linkedlist = new LinkedList(_values); + _hashset = new HashSet(_values); + _queue = new Queue(_values); + _stack = new Stack(_values); + _sortedset = new SortedSet(_values); + _immutablearray = Immutable.ImmutableArray.CreateRange(_values); + _immutablehashset = Immutable.ImmutableHashSet.CreateRange(_values); + _immutablelist = Immutable.ImmutableList.CreateRange(_values); + _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(_values); + } + + [Benchmark] + public bool Array() + { + bool result = default; + var local = _array; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool List() + { + bool result = default; + var local = _list; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool LinkedList() + { + bool result = default; + var local = _linkedlist; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool HashSet() + { + bool result = default; + var local = _hashset; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool Queue() + { + bool result = default; + var local = _queue; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool Stack() + { + bool result = default; + var local = _stack; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool SortedSet() + { + bool result = default; + var local = _sortedset; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool ImmutableArray() + { + bool result = default; + var local = _immutablearray; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool ImmutableHashSet() + { + bool result = default; + var local = _immutablehashset; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool ImmutableList() + { + bool result = default; + var local = _immutablelist; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + + [Benchmark] + public bool ImmutableSortedSet() + { + bool result = default; + var local = _immutablesortedset; + var found = _values; + for (int i = 0; i < found.Length; i++) + result = local.Contains(found[i]); + return result; + } + } +} \ No newline at end of file From aff1094c2db2f74b58ee9b49d692404fad74e0d8 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 12 Jul 2018 13:31:54 +0200 Subject: [PATCH 11/31] ContainsKey for all generic collections --- .../Contains/ContainsKeyFalse.cs | 110 ++++++++++++++++++ .../Contains/ContainsKeyTrue.cs | 108 +++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs create mode 100644 src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs new file mode 100644 index 00000000000..4ee3fada80c --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs @@ -0,0 +1,110 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int), typeof(int))] // value type + [GenericTypeArguments(typeof(string), typeof(string))] // reference type + public class ContainsKeyFalse + { + private TKey[] _notFound; + private Dictionary _source; + + private Dictionary _dictionary; + private SortedList _sortedlist; + private SortedDictionary _sorteddictionary; + private ConcurrentDictionary _concurrentdictionary; + private ImmutableDictionary _immutabledictionary; + private ImmutableSortedDictionary _immutablesorteddictionary; + + [Params(100)] + public int Size; + + [GlobalSetup] + public void Setup() + { + var values = UniqueValuesGenerator.GenerateArray(Size * 2); + _notFound = values.Take(Size).ToArray(); + + _source = values.Skip(Size).Take(Size).ToDictionary(item => item, item => (TValue)(object)item); + _dictionary = new Dictionary(_source); + _sortedlist = new SortedList(_source); + _sorteddictionary = new SortedDictionary(_source); + _concurrentdictionary = new ConcurrentDictionary(_source); + _immutabledictionary = Immutable.ImmutableDictionary.CreateRange(_source); + _immutablesorteddictionary = Immutable.ImmutableSortedDictionary.CreateRange(_source); + } + + [Benchmark] + public bool Dictionary() + { + bool result = default; + var collection = _dictionary; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = collection.ContainsKey(notFound[i]); + return result; + } + + [Benchmark] + public bool SortedList() + { + bool result = default; + var collection = _sortedlist; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = collection.ContainsKey(notFound[i]); + return result; + } + + [Benchmark] + public bool SortedDictionary() + { + bool result = default; + var collection = _sorteddictionary; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = collection.ContainsKey(notFound[i]); + return result; + } + + [Benchmark] + public bool ConcurrentDictionary() + { + bool result = default; + var collection = _concurrentdictionary; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = collection.ContainsKey(notFound[i]); + return result; + } + + [Benchmark] + public bool ImmutableDictionary() + { + bool result = default; + var collection = _immutabledictionary; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = collection.ContainsKey(notFound[i]); + return result; + } + + [Benchmark] + public bool ImmutableSortedDictionary() + { + bool result = default; + var collection = _immutablesorteddictionary; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = collection.ContainsKey(notFound[i]); + return result; + } + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs new file mode 100644 index 00000000000..dafab32be6a --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs @@ -0,0 +1,108 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int), typeof(int))] // value type + [GenericTypeArguments(typeof(string), typeof(string))] // reference type + public class ContainsKeyTrue + { + private TKey[] _array; + private Dictionary _source; + + private Dictionary _dictionary; + private SortedList _sortedlist; + private SortedDictionary _sorteddictionary; + private ConcurrentDictionary _concurrentdictionary; + private ImmutableDictionary _immutabledictionary; + private ImmutableSortedDictionary _immutablesorteddictionary; + + [Params(100)] + public int Size; + + [GlobalSetup] + public void Setup() + { + _array = UniqueValuesGenerator.GenerateArray(Size); + _source = _array.ToDictionary(item => item, item => (TValue)(object)item); + _dictionary = new Dictionary(_source); + _sortedlist = new SortedList(_source); + _sorteddictionary = new SortedDictionary(_source); + _concurrentdictionary = new ConcurrentDictionary(_source); + _immutabledictionary = Immutable.ImmutableDictionary.CreateRange(_source); + _immutablesorteddictionary = Immutable.ImmutableSortedDictionary.CreateRange(_source); + } + + [Benchmark] + public bool Dictionary() + { + bool result = default; + var collection = _dictionary; + var found = _array; + for (int i = 0; i < found.Length; i++) + result = collection.ContainsKey(found[i]); + return result; + } + + [Benchmark] + public bool SortedList() + { + bool result = default; + var collection = _sortedlist; + var found = _array; + for (int i = 0; i < found.Length; i++) + result = collection.ContainsKey(found[i]); + return result; + } + + [Benchmark] + public bool SortedDictionary() + { + bool result = default; + var collection = _sorteddictionary; + var found = _array; + for (int i = 0; i < found.Length; i++) + result = collection.ContainsKey(found[i]); + return result; + } + + [Benchmark] + public bool ConcurrentDictionary() + { + bool result = default; + var collection = _concurrentdictionary; + var found = _array; + for (int i = 0; i < found.Length; i++) + result = collection.ContainsKey(found[i]); + return result; + } + + [Benchmark] + public bool ImmutableDictionary() + { + bool result = default; + var collection = _immutabledictionary; + var found = _array; + for (int i = 0; i < found.Length; i++) + result = collection.ContainsKey(found[i]); + return result; + } + + [Benchmark] + public bool ImmutableSortedDictionary() + { + bool result = default; + var collection = _immutablesorteddictionary; + var found = _array; + for (int i = 0; i < found.Length; i++) + result = collection.ContainsKey(found[i]); + return result; + } + } +} \ No newline at end of file From 6e6840894f844c5ee9cfabc82d18347d4e0c2f50 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 12 Jul 2018 14:29:48 +0200 Subject: [PATCH 12/31] add benchmarks --- .../System.Collections/Add/AddDefaultSize.cs | 92 +++++++++++++++++++ .../System.Collections/Add/AddGivenSize.cs | 63 +++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs create mode 100644 src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs diff --git a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs new file mode 100644 index 00000000000..9c4f4b77ab0 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs @@ -0,0 +1,92 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class AddDefaultSize + { + private T[] _values; + + [Params(100)] + public int Count; + + [GlobalSetup] + public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); + + [Benchmark] + public List List() + { + var result = new List(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i]); + return result; + } + + [Benchmark] + public HashSet HashSet() + { + var result = new HashSet(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i]); + return result; + } + + [Benchmark] + public Dictionary Dictionary() + { + var result = new Dictionary(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i], values[i]); + return result; + } + + [Benchmark] + public SortedList SortedList() + { + var result = new SortedList(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i], values[i]); + return result; + } + + [Benchmark] + public SortedSet SortedSet() + { + var result = new SortedSet(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i]); + return result; + } + + [Benchmark] + public SortedDictionary SortedDictionary() + { + var result = new SortedDictionary(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i], values[i]); + return result; + } + + [Benchmark] + public ConcurrentBag ConcurrentBag() + { + var result = new ConcurrentBag(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i]); + return result; + } + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs new file mode 100644 index 00000000000..3e8f8137f0c --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class AddGivenSize + { + private T[] _values; + + [Params(100)] + public int Count; + + [GlobalSetup] + public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); + + [Benchmark] + public List List() + { + var result = new List(Count); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i]); + return result; + } + +#if !NET461 // API added in .NET Core 2.0 + [Benchmark] + public HashSet HashSet() + { + var result = new HashSet(Count); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i]); + return result; + } +#endif + + [Benchmark] + public Dictionary Dictionary() + { + var result = new Dictionary(Count); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i], values[i]); + return result; + } + + [Benchmark] + public SortedList SortedList() + { + var result = new SortedList(Count); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.Add(values[i], values[i]); + return result; + } + } +} \ No newline at end of file From a95457695cdca2937e489a3c7cd587569ab01111 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 12 Jul 2018 14:44:39 +0200 Subject: [PATCH 13/31] try add benchmarks --- .../Add/TryAddDefaultSize.cs | 44 +++++++++++++++++++ .../System.Collections/Add/TryAddGivenSize.cs | 44 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs create mode 100644 src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs diff --git a/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs new file mode 100644 index 00000000000..67837bc52fd --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Collections.Concurrent; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class TryAddDefaultSize + { + private T[] _values; + + [Params(100)] + public int Count; + + [GlobalSetup] + public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); + +#if !NET461 // API added in .NET Core 2.0 + [Benchmark] + public Dictionary Dictionary() + { + var result = new Dictionary(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.TryAdd(values[i], values[i]); + return result; + } +#endif + + [Benchmark] + public ConcurrentDictionary ConcurrentDictionary() + { + var result = new ConcurrentDictionary(); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.TryAdd(values[i], values[i]); + return result; + } + } +} \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs new file mode 100644 index 00000000000..db6d8293b57 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Collections.Concurrent; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class TryAddGiventSize + { + private T[] _values; + + [Params(100)] + public int Count; + + [GlobalSetup] + public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); + +#if !NET461 // API added in .NET Core 2.0 + [Benchmark] + public Dictionary Dictionary() + { + var result = new Dictionary(Count); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.TryAdd(values[i], values[i]); + return result; + } +#endif + + [Benchmark] + public ConcurrentDictionary ConcurrentDictionary() + { + var result = new ConcurrentDictionary(concurrencyLevel: 4, capacity: Count); + var values = _values; + for(int i = 0; i < values.Length; i++) + result.TryAdd(values[i], values[i]); + return result; + } + } +} \ No newline at end of file From c9a467bd6dd6518328a67c1f25c57cdeeb0c55c7 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 12 Jul 2018 14:57:18 +0200 Subject: [PATCH 14/31] Enqueue and Push benchmarks --- .../System.Collections/Add/AddDefaultSize.cs | 56 ++++++++++++++++--- .../System.Collections/Add/AddGivenSize.cs | 30 ++++++++-- 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs index 9c4f4b77ab0..4202ab79549 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs @@ -18,13 +18,13 @@ public class AddDefaultSize [GlobalSetup] public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); - + [Benchmark] public List List() { var result = new List(); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i]); return result; } @@ -34,7 +34,7 @@ public HashSet HashSet() { var result = new HashSet(); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i]); return result; } @@ -44,7 +44,7 @@ public Dictionary Dictionary() { var result = new Dictionary(); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i], values[i]); return result; } @@ -54,7 +54,7 @@ public SortedList SortedList() { var result = new SortedList(); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i], values[i]); return result; } @@ -64,7 +64,7 @@ public SortedSet SortedSet() { var result = new SortedSet(); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i]); return result; } @@ -74,7 +74,7 @@ public SortedDictionary SortedDictionary() { var result = new SortedDictionary(); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i], values[i]); return result; } @@ -84,9 +84,49 @@ public ConcurrentBag ConcurrentBag() { var result = new ConcurrentBag(); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i]); return result; } + + [Benchmark] + public Queue Queue() + { + var result = new Queue(); + var values = _values; + for (int i = 0; i < values.Length; i++) + result.Enqueue(values[i]); + return result; + } + + [Benchmark] + public Stack Stack() + { + var result = new Stack(); + var values = _values; + for (int i = 0; i < values.Length; i++) + result.Push(values[i]); + return result; + } + + [Benchmark] + public ConcurrentQueue ConcurrentQueue() + { + var result = new ConcurrentQueue(); + var values = _values; + for (int i = 0; i < values.Length; i++) + result.Enqueue(values[i]); + return result; + } + + [Benchmark] + public ConcurrentStack ConcurrentStack() + { + var result = new ConcurrentStack(); + var values = _values; + for (int i = 0; i < values.Length; i++) + result.Push(values[i]); + return result; + } } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs index 3e8f8137f0c..62235e250e4 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs @@ -17,13 +17,13 @@ public class AddGivenSize [GlobalSetup] public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); - + [Benchmark] public List List() { var result = new List(Count); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i]); return result; } @@ -38,14 +38,14 @@ public HashSet HashSet() result.Add(values[i]); return result; } -#endif +#endif [Benchmark] public Dictionary Dictionary() { var result = new Dictionary(Count); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i], values[i]); return result; } @@ -55,9 +55,29 @@ public SortedList SortedList() { var result = new SortedList(Count); var values = _values; - for(int i = 0; i < values.Length; i++) + for (int i = 0; i < values.Length; i++) result.Add(values[i], values[i]); return result; } + + [Benchmark] + public Queue Queue() + { + var result = new Queue(Count); + var values = _values; + for (int i = 0; i < values.Length; i++) + result.Enqueue(values[i]); + return result; + } + + [Benchmark] + public Stack Stack() + { + var result = new Stack(Count); + var values = _values; + for (int i = 0; i < values.Length; i++) + result.Push(values[i]); + return result; + } } } \ No newline at end of file From 8317a315c4f060d37b7a47e3aa560bac1a3c4505 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 12 Jul 2018 16:59:34 +0200 Subject: [PATCH 15/31] Indexer Set benchmarks --- .../corefx/System.Collections/Indexer/Set.cs | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Indexer/Set.cs diff --git a/src/benchmarks/corefx/System.Collections/Indexer/Set.cs b/src/benchmarks/corefx/System.Collections/Indexer/Set.cs new file mode 100644 index 00000000000..4e7212aaed3 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Indexer/Set.cs @@ -0,0 +1,97 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class Set + { + [Params(100)] + public int Size; + + private T[] _keys; + + private T[] _array; + private List _list; + private Dictionary _dictionary; + private SortedList _sortedList; + private SortedDictionary _sortedDictionary; + private ConcurrentDictionary _concurrentDictionary; + + [GlobalSetup] + public void Setup() + { + _keys = UniqueValuesGenerator.GenerateArray(Size); + + _array = _keys.ToArray(); + _list = new List(_keys); + _dictionary = new Dictionary(_keys.ToDictionary(i => i, i => i)); + _sortedList = new SortedList(_keys.ToDictionary(i => i, i => i)); + _sortedDictionary = new SortedDictionary(_keys.ToDictionary(i => i, i => i)); + _concurrentDictionary = new ConcurrentDictionary(_keys.ToDictionary(i => i, i => i)); + } + + [Benchmark] + public T[] Array() + { + var array = _array; + for (int i = 0; i < array.Length; i++) + array[i] = default; + return array; + } + + [Benchmark] + public List List() + { + var list = _list; + for (int i = 0; i < list.Count; i++) + list[i] = default; + return list; + } + + [Benchmark] + public Dictionary Dictionary() + { + var dictionary = _dictionary; + var keys = _keys; + for (int i = 0; i < keys.Length; i++) + dictionary[keys[i]] = default; + return dictionary; + } + [Benchmark] + public SortedList SortedList() + { + var sortedList = _sortedList; + var keys = _keys; + for (int i = 0; i < keys.Length; i++) + sortedList[keys[i]] = default; + return sortedList; + } + + [Benchmark] + public SortedDictionary SortedDictionary() + { + var dictionary = _sortedDictionary; + var keys = _keys; + for (int i = 0; i < keys.Length; i++) + dictionary[keys[i]] = default; + return dictionary; + } + + [Benchmark] + public ConcurrentDictionary ConcurrentDictionary() + { + var dictionary = _concurrentDictionary; + var keys = _keys; + for (int i = 0; i < keys.Length; i++) + dictionary[keys[i]] = default; + return dictionary; + } + } +} \ No newline at end of file From fb4a0feebf211c7d1f98eabff4fb4a652954830f Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 12 Jul 2018 17:47:06 +0200 Subject: [PATCH 16/31] remove benchmarks --- .../System.Collections/Remove/Remove.cs | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Remove/Remove.cs diff --git a/src/benchmarks/corefx/System.Collections/Remove/Remove.cs b/src/benchmarks/corefx/System.Collections/Remove/Remove.cs new file mode 100644 index 00000000000..5ab62424f95 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Remove/Remove.cs @@ -0,0 +1,187 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + [InvocationCount(InvocationsPerIteration)] + public class Remove + { + private const int InvocationsPerIteration = 20000; + + [Params(100)] + public int Size; + + private int _iterationIndex = 0; + private T[] _keys; + + private List[] _lists; + private LinkedList[] _linkedLists; + private HashSet[] _hashSets; + private Dictionary[] _dictionaries; + private SortedList[] _sortedLists; + private SortedSet[] _sortedSets; + private SortedDictionary[] _sortedDictionaries; + private ConcurrentDictionary[] _concurrentDictionaries; + private Stack[] _stacks; + private Queue[] _queues; + private ConcurrentStack[] _concurrentStacks; + private ConcurrentQueue[] _concurrentQueues; + + [GlobalSetup] + public void Setup() => _keys = UniqueValuesGenerator.GenerateArray(Size); + + [IterationCleanup] + public void CleanupIteration() => _iterationIndex = 0; // after every iteration end we set the index to 0 + + [IterationSetup(Target = nameof(List))] + public void SetupListIteration() => _lists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new List(_keys)).ToArray(); + + [Benchmark] + public void List() + { + var list = _lists[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) + list.Remove(key); + } + + [IterationSetup(Target = nameof(LinkedList))] + public void SetupLinkedListIteration() => _linkedLists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new LinkedList(_keys)).ToArray(); + + [Benchmark] + public void LinkedList() + { + var linkedlist = _linkedLists[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) + linkedlist.Remove(key); + } + + [IterationSetup(Target = nameof(HashSet))] + public void SetupHashSetIteration() => _hashSets = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new HashSet(_keys)).ToArray(); + + [Benchmark] + public void HashSet() + { + var hashset = _hashSets[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) + hashset.Remove(key); + } + + [IterationSetup(Target = nameof(Dictionary))] + public void SetupDictionaryIteration() => _dictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Dictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + + [Benchmark] + public void Dictionary() + { + var dictionary = _dictionaries[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) + dictionary.Remove(key); + } + + [IterationSetup(Target = nameof(SortedList))] + public void SetupSortedListIteration() => _sortedLists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedList(_keys.ToDictionary(i => i, i => i))).ToArray(); + + [Benchmark] + public void SortedList() + { + var sortedlist = _sortedLists[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) + sortedlist.Remove(key); + } + + [IterationSetup(Target = nameof(SortedSet))] + public void SetupSortedSetIteration() => _sortedSets = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedSet(_keys)).ToArray(); + + [Benchmark] + public void SortedSet() + { + var sortedset = _sortedSets[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) + sortedset.Remove(key); + } + + [IterationSetup(Target = nameof(SortedDictionary))] + public void SetupSortedDictionaryIteration() => _sortedDictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedDictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + + [Benchmark] + public void SortedDictionary() + { + var sorteddictionary = _sortedDictionaries[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) + sorteddictionary.Remove(key); + } + + [IterationSetup(Target = nameof(ConcurrentDictionary))] + public void SetupConcurrentDictionaryIteration() => _concurrentDictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentDictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + + [Benchmark] + public void ConcurrentDictionary() + { + var concurrentdictionary = _concurrentDictionaries[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) + concurrentdictionary.TryRemove(key, out _); + } + + [IterationSetup(Target = nameof(Stack))] + public void SetupStackIteration() => _stacks = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Stack(_keys)).ToArray(); + + [Benchmark] + public void Stack() + { + var stack = _stacks[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) // we don't need to iterate over keys but to compare apples to apples we do + stack.Pop(); + } + + [IterationSetup(Target = nameof(ConcurrentStack))] + public void SetupConcurrentStackIteration() => _concurrentStacks = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentStack(_keys)).ToArray(); + + [Benchmark] + public void ConcurrentStack() + { + var stack = _concurrentStacks[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) // we don't need to iterate over keys but to compare apples to apples we do + stack.TryPop(out _); + } + + [IterationSetup(Target = nameof(Queue))] + public void SetupQueueIteration() => _queues = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Queue(_keys)).ToArray(); + + [Benchmark] + public void Queue() + { + var queue = _queues[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) // we don't need to iterate over keys but to compare apples to apples we do + queue.Dequeue(); + } + + [IterationSetup(Target = nameof(ConcurrentQueue))] + public void SetupConcurrentQueueIteration() => _concurrentQueues = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentQueue(_keys)).ToArray(); + + [Benchmark] + public void ConcurrentQueue() + { + var queue = _concurrentQueues[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) // we don't need to iterate over keys but to compare apples to apples we do + queue.TryDequeue(out _); + } + } +} \ No newline at end of file From 512d24b31e941569276fe99f1dd9af1ffc6c02cb Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 12 Jul 2018 18:36:31 +0200 Subject: [PATCH 17/31] clear benchmarks --- .../corefx/System.Collections/Remove/Clear.cs | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Remove/Clear.cs diff --git a/src/benchmarks/corefx/System.Collections/Remove/Clear.cs b/src/benchmarks/corefx/System.Collections/Remove/Clear.cs new file mode 100644 index 00000000000..85bb7d839eb --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Remove/Clear.cs @@ -0,0 +1,117 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + [InvocationCount(InvocationsPerIteration)] + public class Clear + { + private const int InvocationsPerIteration = 20000; + + [Params(100)] + public int Size; + + private int _iterationIndex = 0; + private T[] _keys; + + private List[] _lists; + private LinkedList[] _linkedLists; + private HashSet[] _hashSets; + private Dictionary[] _dictionaries; + private SortedList[] _sortedLists; + private SortedSet[] _sortedSets; + private SortedDictionary[] _sortedDictionaries; + private ConcurrentDictionary[] _concurrentDictionaries; + private Stack[] _stacks; + private Queue[] _queues; + private ConcurrentStack[] _concurrentStacks; + private ConcurrentQueue[] _concurrentQueues; + + [GlobalSetup] + public void Setup() => _keys = UniqueValuesGenerator.GenerateArray(Size); + + [IterationCleanup] + public void CleanupIteration() => _iterationIndex = 0; // after every iteration end we set the index to 0 + + [IterationSetup(Target = nameof(List))] + public void SetupListIteration() => _lists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new List(_keys)).ToArray(); + + [Benchmark] + public void List() => _lists[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(LinkedList))] + public void SetupLinkedListIteration() => _linkedLists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new LinkedList(_keys)).ToArray(); + + [Benchmark] + public void LinkedList() => _linkedLists[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(HashSet))] + public void SetupHashSetIteration() => _hashSets = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new HashSet(_keys)).ToArray(); + + [Benchmark] + public void HashSet() => _hashSets[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(Dictionary))] + public void SetupDictionaryIteration() => _dictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Dictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + + [Benchmark] + public void Dictionary() => _dictionaries[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(SortedList))] + public void SetupSortedListIteration() => _sortedLists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedList(_keys.ToDictionary(i => i, i => i))).ToArray(); + + [Benchmark] + public void SortedList() => _sortedLists[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(SortedSet))] + public void SetupSortedSetIteration() => _sortedSets = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedSet(_keys)).ToArray(); + + [Benchmark] + public void SortedSet() => _sortedSets[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(SortedDictionary))] + public void SetupSortedDictionaryIteration() => _sortedDictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedDictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + + [Benchmark] + public void SortedDictionary() => _sortedDictionaries[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(ConcurrentDictionary))] + public void SetupConcurrentDictionaryIteration() => _concurrentDictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentDictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + + [Benchmark] + public void ConcurrentDictionary() => _concurrentDictionaries[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(Stack))] + public void SetupStackIteration() => _stacks = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Stack(_keys)).ToArray(); + + [Benchmark] + public void Stack() => _stacks[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(ConcurrentStack))] + public void SetupConcurrentStackIteration() => _concurrentStacks = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentStack(_keys)).ToArray(); + + [Benchmark] + public void ConcurrentStack() => _concurrentStacks[_iterationIndex++].Clear(); + + [IterationSetup(Target = nameof(Queue))] + public void SetupQueueIteration() => _queues = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Queue(_keys)).ToArray(); + + [Benchmark] + public void Queue() => _queues[_iterationIndex++].Clear(); + +#if !NET461 // API added in .NET Core 2.0 + [IterationSetup(Target = nameof(ConcurrentQueue))] + public void SetupConcurrentQueueIteration() => _concurrentQueues = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentQueue(_keys)).ToArray(); + + [Benchmark] + public void ConcurrentQueue() => _concurrentQueues[_iterationIndex++].Clear(); +#endif + } +} \ No newline at end of file From eb1344f1e02393802e60e31d6b32a746836a5bf2 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Fri, 13 Jul 2018 14:02:39 +0200 Subject: [PATCH 18/31] refactoring and cleanup + set LangVersion to latest --- src/benchmarks/Benchmarks.csproj | 2 +- .../System.Collections/Add/AddDefaultSize.cs | 116 ++++---- .../System.Collections/Add/AddGivenSize.cs | 66 ++--- .../Add/TryAddDefaultSize.cs | 26 +- .../System.Collections/Add/TryAddGivenSize.cs | 26 +- .../Contains/ContainsFalse.cs | 46 +-- .../Contains/ContainsKeyFalse.cs | 2 +- .../Contains/ContainsKeyTrue.cs | 20 +- .../Contains/ContainsTrue.cs | 94 +++--- .../Create/CtorDefaultSize.cs | 50 ++-- .../Create/CtorFromCollection.cs | 58 ++-- .../Create/CtorFromCollectionNonGeneric.cs | 12 +- .../Create/CtorGivenSize.cs | 26 +- .../Create/CtorGivenSizeNonGeneric.cs | 4 +- .../Indexer/{Set.cs => IndexerSet.cs} | 4 +- .../System.Collections/Iterate/IterateFor.cs | 74 ++--- .../Iterate/IterateForEach.cs | 272 +++++++++--------- .../Iterate/IterateForEachNonGeneric.cs | 36 +-- .../Iterate/IterateForNonGeneric.cs | 14 +- .../corefx/System.Collections/Remove/Clear.cs | 31 +- .../System.Collections/Remove/Remove.cs | 28 +- .../corefx/System.Collections/Utils.cs | 81 ++++++ 22 files changed, 586 insertions(+), 502 deletions(-) rename src/benchmarks/corefx/System.Collections/Indexer/{Set.cs => IndexerSet.cs} (97%) create mode 100644 src/benchmarks/corefx/System.Collections/Utils.cs diff --git a/src/benchmarks/Benchmarks.csproj b/src/benchmarks/Benchmarks.csproj index b71e4e6d133..370f0682281 100644 --- a/src/benchmarks/Benchmarks.csproj +++ b/src/benchmarks/Benchmarks.csproj @@ -6,7 +6,7 @@ pdbonly true true - 7.3 + latest diff --git a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs index 4202ab79549..089434c8c9b 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs @@ -11,122 +11,122 @@ namespace System.Collections [GenericTypeArguments(typeof(string))] // reference type public class AddDefaultSize { - private T[] _values; + private T[] _uniqueValues; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Count; [GlobalSetup] - public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); + public void Setup() => _uniqueValues = UniqueValuesGenerator.GenerateArray(Count); [Benchmark] public List List() { - var result = new List(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i]); - return result; + var collection = new List(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i]); + return collection; } [Benchmark] public HashSet HashSet() { - var result = new HashSet(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i]); - return result; + var collection = new HashSet(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i]); + return collection; } [Benchmark] public Dictionary Dictionary() { - var result = new Dictionary(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i], values[i]); - return result; + var collection = new Dictionary(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i], uniqueValues[i]); + return collection; } [Benchmark] public SortedList SortedList() { - var result = new SortedList(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i], values[i]); - return result; + var collection = new SortedList(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i], uniqueValues[i]); + return collection; } [Benchmark] public SortedSet SortedSet() { - var result = new SortedSet(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i]); - return result; + var collection = new SortedSet(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i]); + return collection; } [Benchmark] public SortedDictionary SortedDictionary() { - var result = new SortedDictionary(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i], values[i]); - return result; + var collection = new SortedDictionary(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i], uniqueValues[i]); + return collection; } [Benchmark] public ConcurrentBag ConcurrentBag() { - var result = new ConcurrentBag(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i]); - return result; + var collection = new ConcurrentBag(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i]); + return collection; } [Benchmark] public Queue Queue() { - var result = new Queue(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Enqueue(values[i]); - return result; + var collection = new Queue(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Enqueue(uniqueValues[i]); + return collection; } [Benchmark] public Stack Stack() { - var result = new Stack(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Push(values[i]); - return result; + var collection = new Stack(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Push(uniqueValues[i]); + return collection; } [Benchmark] public ConcurrentQueue ConcurrentQueue() { - var result = new ConcurrentQueue(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Enqueue(values[i]); - return result; + var collection = new ConcurrentQueue(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Enqueue(uniqueValues[i]); + return collection; } [Benchmark] public ConcurrentStack ConcurrentStack() { - var result = new ConcurrentStack(); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Push(values[i]); - return result; + var collection = new ConcurrentStack(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Push(uniqueValues[i]); + return collection; } } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs index 62235e250e4..bd83b0abab5 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs @@ -10,74 +10,74 @@ namespace System.Collections [GenericTypeArguments(typeof(string))] // reference type public class AddGivenSize { - private T[] _values; + private T[] _uniqueValues; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Count; [GlobalSetup] - public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); + public void Setup() => _uniqueValues = UniqueValuesGenerator.GenerateArray(Count); [Benchmark] public List List() { - var result = new List(Count); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i]); - return result; + var collection = new List(Count); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i]); + return collection; } #if !NET461 // API added in .NET Core 2.0 [Benchmark] public HashSet HashSet() { - var result = new HashSet(Count); - var values = _values; - for(int i = 0; i < values.Length; i++) - result.Add(values[i]); - return result; + var collection = new HashSet(Count); + var uniqueValues = _uniqueValues; + for(int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i]); + return collection; } #endif [Benchmark] public Dictionary Dictionary() { - var result = new Dictionary(Count); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i], values[i]); - return result; + var collection = new Dictionary(Count); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i], uniqueValues[i]); + return collection; } [Benchmark] public SortedList SortedList() { - var result = new SortedList(Count); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Add(values[i], values[i]); - return result; + var collection = new SortedList(Count); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i], uniqueValues[i]); + return collection; } [Benchmark] public Queue Queue() { - var result = new Queue(Count); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Enqueue(values[i]); - return result; + var collection = new Queue(Count); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Enqueue(uniqueValues[i]); + return collection; } [Benchmark] public Stack Stack() { - var result = new Stack(Count); - var values = _values; - for (int i = 0; i < values.Length; i++) - result.Push(values[i]); - return result; + var collection = new Stack(Count); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Push(uniqueValues[i]); + return collection; } } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs index 67837bc52fd..ff33589658b 100644 --- a/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs @@ -11,34 +11,34 @@ namespace System.Collections [GenericTypeArguments(typeof(string))] // reference type public class TryAddDefaultSize { - private T[] _values; + private T[] _uniqueValues; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Count; [GlobalSetup] - public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); + public void Setup() => _uniqueValues = UniqueValuesGenerator.GenerateArray(Count); #if !NET461 // API added in .NET Core 2.0 [Benchmark] public Dictionary Dictionary() { - var result = new Dictionary(); - var values = _values; - for(int i = 0; i < values.Length; i++) - result.TryAdd(values[i], values[i]); - return result; + var collection = new Dictionary(); + var uniqueValues = _uniqueValues; + for(int i = 0; i < uniqueValues.Length; i++) + collection.TryAdd(uniqueValues[i], uniqueValues[i]); + return collection; } #endif [Benchmark] public ConcurrentDictionary ConcurrentDictionary() { - var result = new ConcurrentDictionary(); - var values = _values; - for(int i = 0; i < values.Length; i++) - result.TryAdd(values[i], values[i]); - return result; + var collection = new ConcurrentDictionary(); + var uniqueValues = _uniqueValues; + for(int i = 0; i < uniqueValues.Length; i++) + collection.TryAdd(uniqueValues[i], uniqueValues[i]); + return collection; } } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs index db6d8293b57..757447c2138 100644 --- a/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs @@ -11,34 +11,34 @@ namespace System.Collections [GenericTypeArguments(typeof(string))] // reference type public class TryAddGiventSize { - private T[] _values; + private T[] _uniqueValues; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Count; [GlobalSetup] - public void Setup() => _values = UniqueValuesGenerator.GenerateArray(Count); + public void Setup() => _uniqueValues = UniqueValuesGenerator.GenerateArray(Count); #if !NET461 // API added in .NET Core 2.0 [Benchmark] public Dictionary Dictionary() { - var result = new Dictionary(Count); - var values = _values; - for(int i = 0; i < values.Length; i++) - result.TryAdd(values[i], values[i]); - return result; + var collection = new Dictionary(Count); + var uniqueValues = _uniqueValues; + for(int i = 0; i < uniqueValues.Length; i++) + collection.TryAdd(uniqueValues[i], uniqueValues[i]); + return collection; } #endif [Benchmark] public ConcurrentDictionary ConcurrentDictionary() { - var result = new ConcurrentDictionary(concurrencyLevel: 4, capacity: Count); - var values = _values; - for(int i = 0; i < values.Length; i++) - result.TryAdd(values[i], values[i]); - return result; + var collection = new ConcurrentDictionary(Utils.ConcurrencyLevel, Count); + var uniqueValues = _uniqueValues; + for(int i = 0; i < uniqueValues.Length; i++) + collection.TryAdd(uniqueValues[i], uniqueValues[i]); + return collection; } } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs index 8c69325c72f..0531d3d1cef 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs @@ -26,7 +26,7 @@ public class ContainsFalse private ImmutableList _immutablelist; private ImmutableSortedSet _immutablesortedset; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; [GlobalSetup] @@ -53,10 +53,10 @@ public void Setup() public bool Array() { bool result = default; - var local = _array; + var collection = _array; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -64,10 +64,10 @@ public bool Array() public bool List() { bool result = default; - var local = _list; + var collection = _list; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -75,10 +75,10 @@ public bool List() public bool LinkedList() { bool result = default; - var local = _linkedlist; + var collection = _linkedlist; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -86,10 +86,10 @@ public bool LinkedList() public bool HashSet() { bool result = default; - var local = _hashset; + var collection = _hashset; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -97,10 +97,10 @@ public bool HashSet() public bool Queue() { bool result = default; - var local = _queue; + var collection = _queue; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -108,10 +108,10 @@ public bool Queue() public bool Stack() { bool result = default; - var local = _stack; + var collection = _stack; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -119,10 +119,10 @@ public bool Stack() public bool SortedSet() { bool result = default; - var local = _sortedset; + var collection = _sortedset; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -130,10 +130,10 @@ public bool SortedSet() public bool ImmutableArray() { bool result = default; - var local = _immutablearray; + var collection = _immutablearray; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -141,10 +141,10 @@ public bool ImmutableArray() public bool ImmutableHashSet() { bool result = default; - var local = _immutablehashset; + var collection = _immutablehashset; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -152,10 +152,10 @@ public bool ImmutableHashSet() public bool ImmutableList() { bool result = default; - var local = _immutablelist; + var collection = _immutablelist; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } @@ -163,10 +163,10 @@ public bool ImmutableList() public bool ImmutableSortedSet() { bool result = default; - var local = _immutablesortedset; + var collection = _immutablesortedset; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = local.Contains(notFound[i]); + result = collection.Contains(notFound[i]); return result; } } diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs index 4ee3fada80c..5d998e9c478 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs @@ -23,7 +23,7 @@ public class ContainsKeyFalse private ImmutableDictionary _immutabledictionary; private ImmutableSortedDictionary _immutablesorteddictionary; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; [GlobalSetup] diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs index dafab32be6a..115db1ef208 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs @@ -13,7 +13,7 @@ namespace System.Collections [GenericTypeArguments(typeof(string), typeof(string))] // reference type public class ContainsKeyTrue { - private TKey[] _array; + private TKey[] _found; private Dictionary _source; private Dictionary _dictionary; @@ -23,14 +23,14 @@ public class ContainsKeyTrue private ImmutableDictionary _immutabledictionary; private ImmutableSortedDictionary _immutablesorteddictionary; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; [GlobalSetup] public void Setup() { - _array = UniqueValuesGenerator.GenerateArray(Size); - _source = _array.ToDictionary(item => item, item => (TValue)(object)item); + _found = UniqueValuesGenerator.GenerateArray(Size); + _source = _found.ToDictionary(item => item, item => (TValue)(object)item); _dictionary = new Dictionary(_source); _sortedlist = new SortedList(_source); _sorteddictionary = new SortedDictionary(_source); @@ -44,7 +44,7 @@ public bool Dictionary() { bool result = default; var collection = _dictionary; - var found = _array; + var found = _found; for (int i = 0; i < found.Length; i++) result = collection.ContainsKey(found[i]); return result; @@ -55,7 +55,7 @@ public bool SortedList() { bool result = default; var collection = _sortedlist; - var found = _array; + var found = _found; for (int i = 0; i < found.Length; i++) result = collection.ContainsKey(found[i]); return result; @@ -66,7 +66,7 @@ public bool SortedDictionary() { bool result = default; var collection = _sorteddictionary; - var found = _array; + var found = _found; for (int i = 0; i < found.Length; i++) result = collection.ContainsKey(found[i]); return result; @@ -77,7 +77,7 @@ public bool ConcurrentDictionary() { bool result = default; var collection = _concurrentdictionary; - var found = _array; + var found = _found; for (int i = 0; i < found.Length; i++) result = collection.ContainsKey(found[i]); return result; @@ -88,7 +88,7 @@ public bool ImmutableDictionary() { bool result = default; var collection = _immutabledictionary; - var found = _array; + var found = _found; for (int i = 0; i < found.Length; i++) result = collection.ContainsKey(found[i]); return result; @@ -99,7 +99,7 @@ public bool ImmutableSortedDictionary() { bool result = default; var collection = _immutablesorteddictionary; - var found = _array; + var found = _found; for (int i = 0; i < found.Length; i++) result = collection.ContainsKey(found[i]); return result; diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs index 90d4c10c4cf..33d5e074297 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs @@ -12,7 +12,7 @@ namespace System.Collections [GenericTypeArguments(typeof(string))] // reference type public class ContainsTrue { - private T[] _values; + private T[] _found; private T[] _array; private List _list; @@ -26,34 +26,34 @@ public class ContainsTrue private ImmutableList _immutablelist; private ImmutableSortedSet _immutablesortedset; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; [GlobalSetup] public void Setup() { - _values = UniqueValuesGenerator.GenerateArray(Size); - _array = _values.ToArray(); - _list = new List(_values); - _linkedlist = new LinkedList(_values); - _hashset = new HashSet(_values); - _queue = new Queue(_values); - _stack = new Stack(_values); - _sortedset = new SortedSet(_values); - _immutablearray = Immutable.ImmutableArray.CreateRange(_values); - _immutablehashset = Immutable.ImmutableHashSet.CreateRange(_values); - _immutablelist = Immutable.ImmutableList.CreateRange(_values); - _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(_values); + _found = UniqueValuesGenerator.GenerateArray(Size); + _array = _found.ToArray(); + _list = new List(_found); + _linkedlist = new LinkedList(_found); + _hashset = new HashSet(_found); + _queue = new Queue(_found); + _stack = new Stack(_found); + _sortedset = new SortedSet(_found); + _immutablearray = Immutable.ImmutableArray.CreateRange(_found); + _immutablehashset = Immutable.ImmutableHashSet.CreateRange(_found); + _immutablelist = Immutable.ImmutableList.CreateRange(_found); + _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(_found); } [Benchmark] public bool Array() { bool result = default; - var local = _array; - var found = _values; + var collection = _array; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -61,10 +61,10 @@ public bool Array() public bool List() { bool result = default; - var local = _list; - var found = _values; + var collection = _list; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -72,10 +72,10 @@ public bool List() public bool LinkedList() { bool result = default; - var local = _linkedlist; - var found = _values; + var collection = _linkedlist; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -83,10 +83,10 @@ public bool LinkedList() public bool HashSet() { bool result = default; - var local = _hashset; - var found = _values; + var collection = _hashset; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -94,10 +94,10 @@ public bool HashSet() public bool Queue() { bool result = default; - var local = _queue; - var found = _values; + var collection = _queue; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -105,10 +105,10 @@ public bool Queue() public bool Stack() { bool result = default; - var local = _stack; - var found = _values; + var collection = _stack; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -116,10 +116,10 @@ public bool Stack() public bool SortedSet() { bool result = default; - var local = _sortedset; - var found = _values; + var collection = _sortedset; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -127,10 +127,10 @@ public bool SortedSet() public bool ImmutableArray() { bool result = default; - var local = _immutablearray; - var found = _values; + var collection = _immutablearray; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -138,10 +138,10 @@ public bool ImmutableArray() public bool ImmutableHashSet() { bool result = default; - var local = _immutablehashset; - var found = _values; + var collection = _immutablehashset; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -149,10 +149,10 @@ public bool ImmutableHashSet() public bool ImmutableList() { bool result = default; - var local = _immutablelist; - var found = _values; + var collection = _immutablelist; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } @@ -160,10 +160,10 @@ public bool ImmutableList() public bool ImmutableSortedSet() { bool result = default; - var local = _immutablesortedset; - var found = _values; + var collection = _immutablesortedset; + var found = _found; for (int i = 0; i < found.Length; i++) - result = local.Contains(found[i]); + result = collection.Contains(found[i]); return result; } } diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs index 65ac4f4d362..30f4a9f8fef 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs @@ -7,74 +7,74 @@ namespace System.Collections { [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] - [GenericTypeArguments(typeof(int), typeof(int))] // value type - [GenericTypeArguments(typeof(string), typeof(string))] // reference type - public class CtorDefaultSize + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class CtorDefaultSize { [Benchmark] - public TKey[] Array() => new TKey[4]; // array has no default size, List has = 4 so I decided to use 4 here + public T[] Array() => new T[4]; // array has no default size, List has = 4 so I decided to use 4 here [Benchmark] - public List List() => new List(); + public List List() => new List(); [Benchmark] - public LinkedList LinkedList() => new LinkedList(); + public LinkedList LinkedList() => new LinkedList(); [Benchmark] - public HashSet HashSet() => new HashSet(); + public HashSet HashSet() => new HashSet(); [Benchmark] - public Dictionary Dictionary() => new Dictionary(); + public Dictionary Dictionary() => new Dictionary(); [Benchmark] - public Queue Queue() => new Queue(); + public Queue Queue() => new Queue(); [Benchmark] - public Stack Stack() => new Stack(); + public Stack Stack() => new Stack(); [Benchmark] - public SortedList SortedList() => new SortedList(); + public SortedList SortedList() => new SortedList(); [Benchmark] - public SortedSet SortedSet() => new SortedSet(); + public SortedSet SortedSet() => new SortedSet(); [Benchmark] - public SortedDictionary SortedDictionary() => new SortedDictionary(); + public SortedDictionary SortedDictionary() => new SortedDictionary(); [Benchmark] - public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(); + public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(); [Benchmark] - public ConcurrentQueue ConcurrentQueue() => new ConcurrentQueue(); + public ConcurrentQueue ConcurrentQueue() => new ConcurrentQueue(); [Benchmark] - public ConcurrentStack ConcurrentStack() => new ConcurrentStack(); + public ConcurrentStack ConcurrentStack() => new ConcurrentStack(); [Benchmark] - public ConcurrentBag ConcurrentBag() => new ConcurrentBag(); + public ConcurrentBag ConcurrentBag() => new ConcurrentBag(); [Benchmark] - public ImmutableArray ImmutableArray() => Immutable.ImmutableArray.Create(); + public ImmutableArray ImmutableArray() => Immutable.ImmutableArray.Create(); [Benchmark] - public ImmutableDictionary ImmutableDictionary() => Immutable.ImmutableDictionary.Create(); + public ImmutableDictionary ImmutableDictionary() => Immutable.ImmutableDictionary.Create(); [Benchmark] - public ImmutableHashSet ImmutableHashSet() => Immutable.ImmutableHashSet.Create(); + public ImmutableHashSet ImmutableHashSet() => Immutable.ImmutableHashSet.Create(); [Benchmark] - public ImmutableList ImmutableList() => Immutable.ImmutableList.Create(); + public ImmutableList ImmutableList() => Immutable.ImmutableList.Create(); [Benchmark] - public ImmutableQueue ImmutableQueue() => Immutable.ImmutableQueue.Create(); + public ImmutableQueue ImmutableQueue() => Immutable.ImmutableQueue.Create(); [Benchmark] - public ImmutableStack ImmutableStack() => Immutable.ImmutableStack.Create(); + public ImmutableStack ImmutableStack() => Immutable.ImmutableStack.Create(); [Benchmark] - public ImmutableSortedDictionary ImmutableSortedDictionary() => Immutable.ImmutableSortedDictionary.Create(); + public ImmutableSortedDictionary ImmutableSortedDictionary() => Immutable.ImmutableSortedDictionary.Create(); [Benchmark] - public ImmutableSortedSet ImmutableSortedSet() => Immutable.ImmutableSortedSet.Create(); + public ImmutableSortedSet ImmutableSortedSet() => Immutable.ImmutableSortedSet.Create(); } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorFromCollection.cs b/src/benchmarks/corefx/System.Collections/Create/CtorFromCollection.cs index 58f18d36709..ffd56570ccd 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorFromCollection.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorFromCollection.cs @@ -8,84 +8,84 @@ namespace System.Collections { [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] - [GenericTypeArguments(typeof(int), typeof(int))] // value type - [GenericTypeArguments(typeof(string), typeof(string))] // reference type - public class CtorFromCollection + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class CtorFromCollection { - private ICollection _collection; - private IDictionary _dictionary; + private ICollection _collection; + private IDictionary _dictionary; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; [GlobalSetup] public void Setup() { - _collection = UniqueValuesGenerator.GenerateArray(Size); - _dictionary = UniqueValuesGenerator.GenerateDictionary(Size); + _collection = UniqueValuesGenerator.GenerateArray(Size); + _dictionary = UniqueValuesGenerator.GenerateDictionary(Size); } [Benchmark] - public List List() => new List(_collection); + public List List() => new List(_collection); [Benchmark] - public LinkedList LinkedList() => new LinkedList(_collection); + public LinkedList LinkedList() => new LinkedList(_collection); [Benchmark] - public HashSet HashSet() => new HashSet(_collection); + public HashSet HashSet() => new HashSet(_collection); [Benchmark] - public Dictionary Dictionary() => new Dictionary(_dictionary); + public Dictionary Dictionary() => new Dictionary(_dictionary); [Benchmark] - public Queue Queue() => new Queue(_collection); + public Queue Queue() => new Queue(_collection); [Benchmark] - public Stack Stack() => new Stack(_collection); + public Stack Stack() => new Stack(_collection); [Benchmark] - public SortedList SortedList() => new SortedList(_dictionary); + public SortedList SortedList() => new SortedList(_dictionary); [Benchmark] - public SortedSet SortedSet() => new SortedSet(_collection); + public SortedSet SortedSet() => new SortedSet(_collection); [Benchmark] - public SortedDictionary SortedDictionary() => new SortedDictionary(_dictionary); + public SortedDictionary SortedDictionary() => new SortedDictionary(_dictionary); [Benchmark] - public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(_dictionary); + public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(_dictionary); [Benchmark] - public ConcurrentQueue ConcurrentQueue() => new ConcurrentQueue(_collection); + public ConcurrentQueue ConcurrentQueue() => new ConcurrentQueue(_collection); [Benchmark] - public ConcurrentStack ConcurrentStack() => new ConcurrentStack(_collection); + public ConcurrentStack ConcurrentStack() => new ConcurrentStack(_collection); [Benchmark] - public ConcurrentBag ConcurrentBag() => new ConcurrentBag(_collection); + public ConcurrentBag ConcurrentBag() => new ConcurrentBag(_collection); [Benchmark] - public ImmutableArray ImmutableArray() => Immutable.ImmutableArray.CreateRange(_collection); + public ImmutableArray ImmutableArray() => Immutable.ImmutableArray.CreateRange(_collection); [Benchmark] - public ImmutableDictionary ImmutableDictionary() => Immutable.ImmutableDictionary.CreateRange(_dictionary); + public ImmutableDictionary ImmutableDictionary() => Immutable.ImmutableDictionary.CreateRange(_dictionary); [Benchmark] - public ImmutableHashSet ImmutableHashSet() => Immutable.ImmutableHashSet.CreateRange(_collection); + public ImmutableHashSet ImmutableHashSet() => Immutable.ImmutableHashSet.CreateRange(_collection); [Benchmark] - public ImmutableList ImmutableList() => Immutable.ImmutableList.CreateRange(_collection); + public ImmutableList ImmutableList() => Immutable.ImmutableList.CreateRange(_collection); [Benchmark] - public ImmutableQueue ImmutableQueue() => Immutable.ImmutableQueue.CreateRange(_collection); + public ImmutableQueue ImmutableQueue() => Immutable.ImmutableQueue.CreateRange(_collection); [Benchmark] - public ImmutableStack ImmutableStack() => Immutable.ImmutableStack.CreateRange(_collection); + public ImmutableStack ImmutableStack() => Immutable.ImmutableStack.CreateRange(_collection); [Benchmark] - public ImmutableSortedDictionary ImmutableSortedDictionary() => Immutable.ImmutableSortedDictionary.CreateRange(_dictionary); + public ImmutableSortedDictionary ImmutableSortedDictionary() => Immutable.ImmutableSortedDictionary.CreateRange(_dictionary); [Benchmark] - public ImmutableSortedSet ImmutableSortedSet() => Immutable.ImmutableSortedSet.CreateRange(_collection); + public ImmutableSortedSet ImmutableSortedSet() => Immutable.ImmutableSortedSet.CreateRange(_collection); } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorFromCollectionNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Create/CtorFromCollectionNonGeneric.cs index ce6ad18f13f..c55fd788245 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorFromCollectionNonGeneric.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorFromCollectionNonGeneric.cs @@ -4,20 +4,22 @@ namespace System.Collections { - [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] - public class CtorFromCollectionNonGeneric + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.NonGenericCollections)] + [GenericTypeArguments(typeof(int))] // value type (it shows how bad idea is to use non-generic collections for value types) + [GenericTypeArguments(typeof(string))] // reference type + public class CtorFromCollectionNonGeneric { private ICollection _collection; private IDictionary _dictionary; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; [GlobalSetup] public void Setup() { - _collection = UniqueValuesGenerator.GenerateArray(Size); - _dictionary = UniqueValuesGenerator.GenerateDictionary(Size); + _collection = UniqueValuesGenerator.GenerateArray(Size); + _dictionary = UniqueValuesGenerator.GenerateDictionary(Size); } [Benchmark] diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs index e03afaa66c4..3ce81c972d6 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs @@ -6,38 +6,36 @@ namespace System.Collections { [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] - [GenericTypeArguments(typeof(int), typeof(int))] // value type - [GenericTypeArguments(typeof(string), typeof(string))] // reference type - public class CtorGiventSize + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class CtorGiventSize { - private const int ConcurrencyLevel = 4; - - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; [Benchmark] - public TKey[] Array() => new TKey[Size]; + public T[] Array() => new T[Size]; [Benchmark] - public List List() => new List(Size); + public List List() => new List(Size); #if !NET461 // API added in .NET Core 2.0 [Benchmark] - public HashSet HashSet() => new HashSet(Size); + public HashSet HashSet() => new HashSet(Size); #endif [Benchmark] - public Dictionary Dictionary() => new Dictionary(Size); + public Dictionary Dictionary() => new Dictionary(Size); [Benchmark] - public Queue Queue() => new Queue(Size); + public Queue Queue() => new Queue(Size); [Benchmark] - public Stack Stack() => new Stack(Size); + public Stack Stack() => new Stack(Size); [Benchmark] - public SortedList SortedList() => new SortedList(Size); + public SortedList SortedList() => new SortedList(Size); [Benchmark] - public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(ConcurrencyLevel, Size); + public ConcurrentDictionary ConcurrentDictionary() => new ConcurrentDictionary(Utils.ConcurrencyLevel, Size); } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorGivenSizeNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSizeNonGeneric.cs index f07fda2e85f..98fa0b6734b 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorGivenSizeNonGeneric.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSizeNonGeneric.cs @@ -3,10 +3,10 @@ namespace System.Collections { - [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.NonGenericCollections)] public class CtorGivenSizeNonGeneric { - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; [Benchmark] diff --git a/src/benchmarks/corefx/System.Collections/Indexer/Set.cs b/src/benchmarks/corefx/System.Collections/Indexer/IndexerSet.cs similarity index 97% rename from src/benchmarks/corefx/System.Collections/Indexer/Set.cs rename to src/benchmarks/corefx/System.Collections/Indexer/IndexerSet.cs index 4e7212aaed3..06c8d49a835 100644 --- a/src/benchmarks/corefx/System.Collections/Indexer/Set.cs +++ b/src/benchmarks/corefx/System.Collections/Indexer/IndexerSet.cs @@ -10,9 +10,9 @@ namespace System.Collections [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] [GenericTypeArguments(typeof(int))] // value type [GenericTypeArguments(typeof(string))] // reference type - public class Set + public class IndexerSet { - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; private T[] _keys; diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs index 1ce09514b06..5edaef7472f 100644 --- a/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs @@ -9,79 +9,79 @@ namespace System.Collections [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] [GenericTypeArguments(typeof(int))] // value type [GenericTypeArguments(typeof(string))] // reference type - public class IterateFor + public class IterateFor { - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; - private TValue[] _array; - private List _list; - private ImmutableArray _immutablearray; - private ImmutableList _immutablelist; - private ImmutableSortedSet _immutablesortedset; + private T[] _array; + private List _list; + private ImmutableArray _immutablearray; + private ImmutableList _immutablelist; + private ImmutableSortedSet _immutablesortedset; [GlobalSetup(Target = nameof(Array))] - public void SetupArray() => _array = UniqueValuesGenerator.GenerateArray(Size); + public void SetupArray() => _array = UniqueValuesGenerator.GenerateArray(Size); [Benchmark] - public TValue Array() + public T Array() { - TValue result = default; - var local = _array; - for (int i = 0; i < local.Length; i++) - result = local[i]; + T result = default; + var collection = _array; + for (int i = 0; i < collection.Length; i++) + result = collection[i]; return result; } [GlobalSetup(Target = nameof(List))] - public void SetupList() => _list = new List(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupList() => _list = new List(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue List() + public T List() { - TValue result = default; - var local = _list; - for(int i = 0; i < local.Count; i++) - result = local[i]; + T result = default; + var collection = _list; + for(int i = 0; i < collection.Count; i++) + result = collection[i]; return result;; } [GlobalSetup(Target = nameof(ImmutableArray))] - public void SetupImmutableArray() => _immutablearray = Immutable.ImmutableArray.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableArray() => _immutablearray = Immutable.ImmutableArray.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableArray() + public T ImmutableArray() { - TValue result = default; - var local = _immutablearray; - for(int i = 0; i < local.Length; i++) - result = local[i]; + T result = default; + var collection = _immutablearray; + for(int i = 0; i < collection.Length; i++) + result = collection[i]; return result; } [GlobalSetup(Target = nameof(ImmutableList))] - public void SetupImmutableList() => _immutablelist = Immutable.ImmutableList.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableList() => _immutablelist = Immutable.ImmutableList.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableList() + public T ImmutableList() { - TValue result = default; - var local = _immutablelist; - for(int i = 0; i < local.Count; i++) - result = local[i]; + T result = default; + var collection = _immutablelist; + for(int i = 0; i < collection.Count; i++) + result = collection[i]; return result; } [GlobalSetup(Target = nameof(ImmutableSortedSet))] - public void SetupImmutableSortedSet() => _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableSortedSet() => _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableSortedSet() + public T ImmutableSortedSet() { - TValue result = default; - var local = _immutablesortedset; - for(int i = 0; i < local.Count; i++) - result = local[i]; + T result = default; + var collection = _immutablesortedset; + for(int i = 0; i < collection.Count; i++) + result = collection[i]; return result; } } diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs index 04817172f92..dd2e9a21360 100644 --- a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs @@ -8,318 +8,318 @@ namespace System.Collections { [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] - [GenericTypeArguments(typeof(int), typeof(int))] // value type - [GenericTypeArguments(typeof(string), typeof(string))] // reference type - public class IterateForEach + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class IterateForEach { - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; - private TValue[] _array; - private List _list; - private LinkedList _linkedlist; - private HashSet _hashset; - private Dictionary _dictionary; - private Queue _queue; - private Stack _stack; - private SortedList _sortedlist; - private SortedSet _sortedset; - private SortedDictionary _sorteddictionary; - private ConcurrentDictionary _concurrentdictionary; - private ConcurrentQueue _concurrentqueue; - private ConcurrentStack _concurrentstack; - private ConcurrentBag _concurrentbag; - private ImmutableArray _immutablearray; - private ImmutableDictionary _immutabledictionary; - private ImmutableHashSet _immutablehashset; - private ImmutableList _immutablelist; - private ImmutableQueue _immutablequeue; - private ImmutableStack _immutablestack; - private ImmutableSortedDictionary _immutablesorteddictionary; - private ImmutableSortedSet _immutablesortedset; + private T[] _array; + private List _list; + private LinkedList _linkedlist; + private HashSet _hashset; + private Dictionary _dictionary; + private Queue _queue; + private Stack _stack; + private SortedList _sortedlist; + private SortedSet _sortedset; + private SortedDictionary _sorteddictionary; + private ConcurrentDictionary _concurrentdictionary; + private ConcurrentQueue _concurrentqueue; + private ConcurrentStack _concurrentstack; + private ConcurrentBag _concurrentbag; + private ImmutableArray _immutablearray; + private ImmutableDictionary _immutabledictionary; + private ImmutableHashSet _immutablehashset; + private ImmutableList _immutablelist; + private ImmutableQueue _immutablequeue; + private ImmutableStack _immutablestack; + private ImmutableSortedDictionary _immutablesorteddictionary; + private ImmutableSortedSet _immutablesortedset; [GlobalSetup(Target = nameof(Array))] - public void SetupArray() => _array = UniqueValuesGenerator.GenerateArray(Size); + public void SetupArray() => _array = UniqueValuesGenerator.GenerateArray(Size); [Benchmark] - public TValue Array() + public T Array() { - TValue result = default; - var local = _array; - foreach (var item in local) + T result = default; + var collection = _array; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(List))] - public void SetupList() => _list = new List(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupList() => _list = new List(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue List() + public T List() { - TValue result = default; - var local = _list; - foreach (var item in local) + T result = default; + var collection = _list; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(LinkedList))] - public void SetupLinkedList() => _linkedlist = new LinkedList(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupLinkedList() => _linkedlist = new LinkedList(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue LinkedList() + public T LinkedList() { - TValue result = default; - var local = _linkedlist; - foreach (var item in local) + T result = default; + var collection = _linkedlist; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(HashSet))] - public void SetupHashSet() => _hashset = new HashSet(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupHashSet() => _hashset = new HashSet(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue HashSet() + public T HashSet() { - TValue result = default; - var local = _hashset; - foreach (var item in local) + T result = default; + var collection = _hashset; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(Dictionary))] - public void SetupDictionary() => _dictionary = new Dictionary(UniqueValuesGenerator.GenerateDictionary(Size)); + public void SetupDictionary() => _dictionary = new Dictionary(UniqueValuesGenerator.GenerateDictionary(Size)); [Benchmark] - public TValue Dictionary() + public T Dictionary() { - TValue result = default; - var local = _dictionary; - foreach (var item in local) + T result = default; + var collection = _dictionary; + foreach (var item in collection) result = item.Value; return result; } [GlobalSetup(Target = nameof(Queue))] - public void SetupQueue() => _queue = new Queue(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupQueue() => _queue = new Queue(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue Queue() + public T Queue() { - TValue result = default; - var local = _queue; - foreach (var item in local) + T result = default; + var collection = _queue; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(Stack))] - public void SetupStack() => _stack = new Stack(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupStack() => _stack = new Stack(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue Stack() + public T Stack() { - TValue result = default; - var local = _stack; - foreach (var item in local) + T result = default; + var collection = _stack; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(SortedList))] - public void SetupSortedList() => _sortedlist = new SortedList(UniqueValuesGenerator.GenerateDictionary(Size)); + public void SetupSortedList() => _sortedlist = new SortedList(UniqueValuesGenerator.GenerateDictionary(Size)); [Benchmark] - public TValue SortedList() + public T SortedList() { - TValue result = default; - var local = _sortedlist; - foreach (var item in local) + T result = default; + var collection = _sortedlist; + foreach (var item in collection) result = item.Value; return result; } [GlobalSetup(Target = nameof(SortedSet))] - public void SetupSortedSet() => _sortedset = new SortedSet(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupSortedSet() => _sortedset = new SortedSet(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue SortedSet() + public T SortedSet() { - TValue result = default; - var local = _sortedset; - foreach (var item in local) + T result = default; + var collection = _sortedset; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(SortedDictionary))] - public void SetupSortedDictionary() => _sorteddictionary = new SortedDictionary(UniqueValuesGenerator.GenerateDictionary(Size)); + public void SetupSortedDictionary() => _sorteddictionary = new SortedDictionary(UniqueValuesGenerator.GenerateDictionary(Size)); [Benchmark] - public TValue SortedDictionary() + public T SortedDictionary() { - TValue result = default; - var local = _sorteddictionary; - foreach (var item in local) + T result = default; + var collection = _sorteddictionary; + foreach (var item in collection) result = item.Value; return result; } [GlobalSetup(Target = nameof(ConcurrentDictionary))] - public void SetupConcurrentDictionary() => _concurrentdictionary = new ConcurrentDictionary(UniqueValuesGenerator.GenerateDictionary(Size)); + public void SetupConcurrentDictionary() => _concurrentdictionary = new ConcurrentDictionary(UniqueValuesGenerator.GenerateDictionary(Size)); [Benchmark] - public TValue ConcurrentDictionary() + public T ConcurrentDictionary() { - TValue result = default; - var local = _concurrentdictionary; - foreach (var item in local) + T result = default; + var collection = _concurrentdictionary; + foreach (var item in collection) result = item.Value; return result; } [GlobalSetup(Target = nameof(ConcurrentQueue))] - public void SetupConcurrentQueue() => _concurrentqueue = new ConcurrentQueue(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupConcurrentQueue() => _concurrentqueue = new ConcurrentQueue(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ConcurrentQueue() + public T ConcurrentQueue() { - TValue result = default; - var local = _concurrentqueue; - foreach (var item in local) + T result = default; + var collection = _concurrentqueue; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(ConcurrentStack))] - public void SetupConcurrentStack() => _concurrentstack = new ConcurrentStack(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupConcurrentStack() => _concurrentstack = new ConcurrentStack(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ConcurrentStack() + public T ConcurrentStack() { - TValue result = default; - var local = _concurrentstack; - foreach (var item in local) + T result = default; + var collection = _concurrentstack; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(ConcurrentBag))] - public void SetupConcurrentBag() => _concurrentbag = new ConcurrentBag(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupConcurrentBag() => _concurrentbag = new ConcurrentBag(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ConcurrentBag() + public T ConcurrentBag() { - TValue result = default; - var local = _concurrentbag; - foreach (var item in local) + T result = default; + var collection = _concurrentbag; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(ImmutableArray))] - public void SetupImmutableArray() => _immutablearray = Immutable.ImmutableArray.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableArray() => _immutablearray = Immutable.ImmutableArray.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableArray() + public T ImmutableArray() { - TValue result = default; - var local = _immutablearray; - foreach (var item in local) + T result = default; + var collection = _immutablearray; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(ImmutableDictionary))] - public void SetupImmutableDictionary() => _immutabledictionary = Immutable.ImmutableDictionary.CreateRange(UniqueValuesGenerator.GenerateDictionary(Size)); + public void SetupImmutableDictionary() => _immutabledictionary = Immutable.ImmutableDictionary.CreateRange(UniqueValuesGenerator.GenerateDictionary(Size)); [Benchmark] - public TValue ImmutableDictionary() + public T ImmutableDictionary() { - TValue result = default; - var local = _immutabledictionary; - foreach (var item in local) + T result = default; + var collection = _immutabledictionary; + foreach (var item in collection) result = item.Value; return result; } [GlobalSetup(Target = nameof(ImmutableHashSet))] - public void SetupImmutableHashSet() => _immutablehashset = Immutable.ImmutableHashSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableHashSet() => _immutablehashset = Immutable.ImmutableHashSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableHashSet() + public T ImmutableHashSet() { - TValue result = default; - var local = _immutablehashset; - foreach (var item in local) + T result = default; + var collection = _immutablehashset; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(ImmutableList))] - public void SetupImmutableList() => _immutablelist = Immutable.ImmutableList.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableList() => _immutablelist = Immutable.ImmutableList.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableList() + public T ImmutableList() { - TValue result = default; - var local = _immutablelist; - foreach (var item in local) + T result = default; + var collection = _immutablelist; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(ImmutableQueue))] - public void SetupImmutableQueue() => _immutablequeue = Immutable.ImmutableQueue.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableQueue() => _immutablequeue = Immutable.ImmutableQueue.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableQueue() + public T ImmutableQueue() { - TValue result = default; - var local = _immutablequeue; - foreach (var item in local) + T result = default; + var collection = _immutablequeue; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(ImmutableStack))] - public void SetupImmutableStack() => _immutablestack = Immutable.ImmutableStack.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableStack() => _immutablestack = Immutable.ImmutableStack.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableStack() + public T ImmutableStack() { - TValue result = default; - var local = _immutablestack; - foreach (var item in local) + T result = default; + var collection = _immutablestack; + foreach (var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(ImmutableSortedDictionary))] - public void SetupImmutableSortedDictionary() => _immutablesorteddictionary = Immutable.ImmutableSortedDictionary.CreateRange(UniqueValuesGenerator.GenerateDictionary(Size)); + public void SetupImmutableSortedDictionary() => _immutablesorteddictionary = Immutable.ImmutableSortedDictionary.CreateRange(UniqueValuesGenerator.GenerateDictionary(Size)); [Benchmark] - public TValue ImmutableSortedDictionary() + public T ImmutableSortedDictionary() { - TValue result = default; - var local = _immutablesorteddictionary; - foreach (var item in local) + T result = default; + var collection = _immutablesorteddictionary; + foreach (var item in collection) result = item.Value; return result; } [GlobalSetup(Target = nameof(ImmutableSortedSet))] - public void SetupImmutableSortedSet() => _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupImmutableSortedSet() => _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] - public TValue ImmutableSortedSet() + public T ImmutableSortedSet() { - TValue result = default; - var local = _immutablesortedset; - foreach (var item in local) + T result = default; + var collection = _immutablesortedset; + foreach (var item in collection) result = item; return result; } diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs index 59e4aca9173..cb7ff3c2a3b 100644 --- a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEachNonGeneric.cs @@ -5,9 +5,11 @@ namespace System.Collections { [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.NonGenericCollections)] - public class IterateForEachNonGeneric + [GenericTypeArguments(typeof(int))] // value type (it shows how bad idea is to use non-generic collections for value types) + [GenericTypeArguments(typeof(string))] // reference type + public class IterateForEachNonGeneric { - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; private ArrayList _arraylist; @@ -17,66 +19,66 @@ public class IterateForEachNonGeneric private SortedList _sortedlist; [GlobalSetup(Target = nameof(ArrayList))] - public void SetupArrayList() => _arraylist = new ArrayList(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupArrayList() => _arraylist = new ArrayList(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] public object ArrayList() { object result = default; - var local = _arraylist; - foreach(var item in local) + var collection = _arraylist; + foreach(var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(Hashtable))] - public void SetupHashtable() => _hashtable = new Hashtable(UniqueValuesGenerator.GenerateDictionary(Size)); + public void SetupHashtable() => _hashtable = new Hashtable(UniqueValuesGenerator.GenerateDictionary(Size)); [Benchmark] public object Hashtable() { object result = default; - var local = _hashtable; - foreach(var item in local) + var collection = _hashtable; + foreach(var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(Queue))] - public void SetupQueue() => _queue = new Queue(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupQueue() => _queue = new Queue(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] public object Queue() { object result = default; - var local = _queue; - foreach(var item in local) + var collection = _queue; + foreach(var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(Stack))] - public void SetupStack() => _stack = new Stack(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupStack() => _stack = new Stack(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] public object Stack() { object result = default; - var local = _stack; - foreach(var item in local) + var collection = _stack; + foreach(var item in collection) result = item; return result; } [GlobalSetup(Target = nameof(SortedList))] - public void SetupSortedList() => _sortedlist = new SortedList(UniqueValuesGenerator.GenerateDictionary(Size)); + public void SetupSortedList() => _sortedlist = new SortedList(UniqueValuesGenerator.GenerateDictionary(Size)); [Benchmark] public object SortedList() { object result = default; - var local = _sortedlist; - foreach(var item in local) + var collection = _sortedlist; + foreach(var item in collection) result = item; return result; } diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateForNonGeneric.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateForNonGeneric.cs index 09981908940..9bd6141d350 100644 --- a/src/benchmarks/corefx/System.Collections/Iterate/IterateForNonGeneric.cs +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateForNonGeneric.cs @@ -5,23 +5,25 @@ namespace System.Collections { [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.NonGenericCollections)] - public class IterateForNonGeneric + [GenericTypeArguments(typeof(int))] // value type (it shows how bad idea is to use non-generic collections for value types) + [GenericTypeArguments(typeof(string))] // reference type + public class IterateForNonGeneric { - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; private ArrayList _arraylist; [GlobalSetup(Target = nameof(ArrayList))] - public void SetupArrayList() => _arraylist = new ArrayList(UniqueValuesGenerator.GenerateArray(Size)); + public void SetupArrayList() => _arraylist = new ArrayList(UniqueValuesGenerator.GenerateArray(Size)); [Benchmark] public object ArrayList() { object result = default; - var local = _arraylist; - for(int i = 0; i < local.Count; i++) - result = local[i]; + var collection = _arraylist; + for(int i = 0; i < collection.Count; i++) + result = collection[i]; return result; } } diff --git a/src/benchmarks/corefx/System.Collections/Remove/Clear.cs b/src/benchmarks/corefx/System.Collections/Remove/Clear.cs index 85bb7d839eb..657ea9318e0 100644 --- a/src/benchmarks/corefx/System.Collections/Remove/Clear.cs +++ b/src/benchmarks/corefx/System.Collections/Remove/Clear.cs @@ -1,6 +1,5 @@ using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -13,9 +12,9 @@ namespace System.Collections [InvocationCount(InvocationsPerIteration)] public class Clear { - private const int InvocationsPerIteration = 20000; + private const int InvocationsPerIteration = 1000; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; private int _iterationIndex = 0; @@ -41,74 +40,74 @@ public class Clear public void CleanupIteration() => _iterationIndex = 0; // after every iteration end we set the index to 0 [IterationSetup(Target = nameof(List))] - public void SetupListIteration() => _lists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new List(_keys)).ToArray(); + public void SetupListIteration() => Utils.FillCollections(ref _lists, InvocationsPerIteration, _keys); [Benchmark] public void List() => _lists[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(LinkedList))] - public void SetupLinkedListIteration() => _linkedLists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new LinkedList(_keys)).ToArray(); + public void SetupLinkedListIteration() => Utils.FillCollections(ref _linkedLists, InvocationsPerIteration, _keys); [Benchmark] public void LinkedList() => _linkedLists[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(HashSet))] - public void SetupHashSetIteration() => _hashSets = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new HashSet(_keys)).ToArray(); + public void SetupHashSetIteration() => Utils.FillCollections(ref _hashSets, InvocationsPerIteration, _keys); [Benchmark] public void HashSet() => _hashSets[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(Dictionary))] - public void SetupDictionaryIteration() => _dictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Dictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + public void SetupDictionaryIteration() => Utils.FillDictionaries(ref _dictionaries, InvocationsPerIteration, _keys); [Benchmark] public void Dictionary() => _dictionaries[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(SortedList))] - public void SetupSortedListIteration() => _sortedLists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedList(_keys.ToDictionary(i => i, i => i))).ToArray(); + public void SetupSortedListIteration() => Utils.FillDictionaries(ref _sortedLists, InvocationsPerIteration, _keys); [Benchmark] public void SortedList() => _sortedLists[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(SortedSet))] - public void SetupSortedSetIteration() => _sortedSets = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedSet(_keys)).ToArray(); + public void SetupSortedSetIteration() => Utils.FillCollections(ref _sortedSets, InvocationsPerIteration, _keys); [Benchmark] public void SortedSet() => _sortedSets[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(SortedDictionary))] - public void SetupSortedDictionaryIteration() => _sortedDictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedDictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + public void SetupSortedDictionaryIteration() => Utils.FillDictionaries(ref _sortedDictionaries, InvocationsPerIteration, _keys); [Benchmark] public void SortedDictionary() => _sortedDictionaries[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(ConcurrentDictionary))] - public void SetupConcurrentDictionaryIteration() => _concurrentDictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentDictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + public void SetupConcurrentDictionaryIteration() => Utils.FillDictionaries(ref _concurrentDictionaries, InvocationsPerIteration, _keys); [Benchmark] public void ConcurrentDictionary() => _concurrentDictionaries[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(Stack))] - public void SetupStackIteration() => _stacks = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Stack(_keys)).ToArray(); + public void SetupStackIteration() => Utils.FillStacks(ref _stacks, InvocationsPerIteration, _keys); [Benchmark] public void Stack() => _stacks[_iterationIndex++].Clear(); [IterationSetup(Target = nameof(ConcurrentStack))] - public void SetupConcurrentStackIteration() => _concurrentStacks = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentStack(_keys)).ToArray(); + public void SetupConcurrentStackIteration() => Utils.FillProducerConsumerCollection(ref _concurrentStacks, InvocationsPerIteration, _keys); [Benchmark] public void ConcurrentStack() => _concurrentStacks[_iterationIndex++].Clear(); - + [IterationSetup(Target = nameof(Queue))] - public void SetupQueueIteration() => _queues = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Queue(_keys)).ToArray(); + public void SetupQueueIteration() => Utils.FillQueues(ref _queues, InvocationsPerIteration, _keys); [Benchmark] public void Queue() => _queues[_iterationIndex++].Clear(); #if !NET461 // API added in .NET Core 2.0 [IterationSetup(Target = nameof(ConcurrentQueue))] - public void SetupConcurrentQueueIteration() => _concurrentQueues = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentQueue(_keys)).ToArray(); + public void SetupConcurrentQueueIteration() => Utils.FillProducerConsumerCollection(ref _concurrentQueues, InvocationsPerIteration, _keys); [Benchmark] public void ConcurrentQueue() => _concurrentQueues[_iterationIndex++].Clear(); diff --git a/src/benchmarks/corefx/System.Collections/Remove/Remove.cs b/src/benchmarks/corefx/System.Collections/Remove/Remove.cs index 5ab62424f95..096422d264b 100644 --- a/src/benchmarks/corefx/System.Collections/Remove/Remove.cs +++ b/src/benchmarks/corefx/System.Collections/Remove/Remove.cs @@ -13,9 +13,9 @@ namespace System.Collections [InvocationCount(InvocationsPerIteration)] public class Remove { - private const int InvocationsPerIteration = 20000; + private const int InvocationsPerIteration = 1000; - [Params(100)] + [Params(Utils.DefaultCollectionSize)] public int Size; private int _iterationIndex = 0; @@ -41,7 +41,7 @@ public class Remove public void CleanupIteration() => _iterationIndex = 0; // after every iteration end we set the index to 0 [IterationSetup(Target = nameof(List))] - public void SetupListIteration() => _lists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new List(_keys)).ToArray(); + public void SetupListIteration() => Utils.FillCollections(ref _lists, InvocationsPerIteration, _keys); [Benchmark] public void List() @@ -53,7 +53,7 @@ public void List() } [IterationSetup(Target = nameof(LinkedList))] - public void SetupLinkedListIteration() => _linkedLists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new LinkedList(_keys)).ToArray(); + public void SetupLinkedListIteration() => Utils.FillCollections(ref _linkedLists, InvocationsPerIteration, _keys); [Benchmark] public void LinkedList() @@ -65,7 +65,7 @@ public void LinkedList() } [IterationSetup(Target = nameof(HashSet))] - public void SetupHashSetIteration() => _hashSets = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new HashSet(_keys)).ToArray(); + public void SetupHashSetIteration() => Utils.FillCollections(ref _hashSets, InvocationsPerIteration, _keys); [Benchmark] public void HashSet() @@ -77,7 +77,7 @@ public void HashSet() } [IterationSetup(Target = nameof(Dictionary))] - public void SetupDictionaryIteration() => _dictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Dictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + public void SetupDictionaryIteration() => Utils.FillDictionaries(ref _dictionaries, InvocationsPerIteration, _keys); [Benchmark] public void Dictionary() @@ -89,7 +89,7 @@ public void Dictionary() } [IterationSetup(Target = nameof(SortedList))] - public void SetupSortedListIteration() => _sortedLists = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedList(_keys.ToDictionary(i => i, i => i))).ToArray(); + public void SetupSortedListIteration() => Utils.FillDictionaries(ref _sortedLists, InvocationsPerIteration, _keys); [Benchmark] public void SortedList() @@ -101,7 +101,7 @@ public void SortedList() } [IterationSetup(Target = nameof(SortedSet))] - public void SetupSortedSetIteration() => _sortedSets = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedSet(_keys)).ToArray(); + public void SetupSortedSetIteration() => Utils.FillCollections(ref _sortedSets, InvocationsPerIteration, _keys); [Benchmark] public void SortedSet() @@ -113,7 +113,7 @@ public void SortedSet() } [IterationSetup(Target = nameof(SortedDictionary))] - public void SetupSortedDictionaryIteration() => _sortedDictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new SortedDictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + public void SetupSortedDictionaryIteration() => Utils.FillDictionaries(ref _sortedDictionaries, InvocationsPerIteration, _keys); [Benchmark] public void SortedDictionary() @@ -125,7 +125,7 @@ public void SortedDictionary() } [IterationSetup(Target = nameof(ConcurrentDictionary))] - public void SetupConcurrentDictionaryIteration() => _concurrentDictionaries = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentDictionary(_keys.ToDictionary(i => i, i => i))).ToArray(); + public void SetupConcurrentDictionaryIteration() => Utils.FillDictionaries(ref _concurrentDictionaries, InvocationsPerIteration, _keys); [Benchmark] public void ConcurrentDictionary() @@ -137,7 +137,7 @@ public void ConcurrentDictionary() } [IterationSetup(Target = nameof(Stack))] - public void SetupStackIteration() => _stacks = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Stack(_keys)).ToArray(); + public void SetupStackIteration() => Utils.FillStacks(ref _stacks, InvocationsPerIteration, _keys); [Benchmark] public void Stack() @@ -149,7 +149,7 @@ public void Stack() } [IterationSetup(Target = nameof(ConcurrentStack))] - public void SetupConcurrentStackIteration() => _concurrentStacks = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentStack(_keys)).ToArray(); + public void SetupConcurrentStackIteration() => Utils.FillProducerConsumerCollection(ref _concurrentStacks, InvocationsPerIteration, _keys); [Benchmark] public void ConcurrentStack() @@ -161,7 +161,7 @@ public void ConcurrentStack() } [IterationSetup(Target = nameof(Queue))] - public void SetupQueueIteration() => _queues = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new Queue(_keys)).ToArray(); + public void SetupQueueIteration() => Utils.FillQueues(ref _queues, InvocationsPerIteration, _keys); [Benchmark] public void Queue() @@ -173,7 +173,7 @@ public void Queue() } [IterationSetup(Target = nameof(ConcurrentQueue))] - public void SetupConcurrentQueueIteration() => _concurrentQueues = Enumerable.Range(0, InvocationsPerIteration).Select(_ => new ConcurrentQueue(_keys)).ToArray(); + public void SetupConcurrentQueueIteration() => Utils.FillProducerConsumerCollection(ref _concurrentQueues, InvocationsPerIteration, _keys); [Benchmark] public void ConcurrentQueue() diff --git a/src/benchmarks/corefx/System.Collections/Utils.cs b/src/benchmarks/corefx/System.Collections/Utils.cs new file mode 100644 index 00000000000..b9afa6d4789 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Utils.cs @@ -0,0 +1,81 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace System.Collections +{ + internal static class Utils + { + internal const int DefaultCollectionSize = 512; + + internal const int ConcurrencyLevel = 4; + + internal static void FillCollections(ref TCollection[] collections, int collectionsCount, TValues[] keys) + where TCollection : ICollection, new() + { + if (collections == null) + collections = Enumerable.Range(0, collectionsCount).Select(_ => new TCollection()).ToArray(); + + foreach (var collection in collections.Where(collection => collection.Count < keys.Length)) + foreach (var value in keys) + collection.Add(value); + + if(collections.Any(collection => collection.Count != keys.Length)) // we dont use Debug.Assert here because this code will be executed mostly in Release + throw new InvalidOperationException(); + } + + internal static void FillDictionaries(ref TCollection[] collections, int collectionsCount, TValues[] keys) + where TCollection : IDictionary, new() + { + if (collections == null) + collections = Enumerable.Range(0, collectionsCount).Select(_ => new TCollection()).ToArray(); + + foreach (var collection in collections.Where(collection => collection.Count < keys.Length)) + foreach (var key in keys) + collection.Add(key, key); + + if(collections.Any(collection => collection.Count != keys.Length)) + throw new InvalidOperationException(); + } + + internal static void FillProducerConsumerCollection(ref TCollection[] collections, int collectionsCount, TValues[] keys) + where TCollection : IProducerConsumerCollection, new() + { + if (collections == null) + collections = Enumerable.Range(0, collectionsCount).Select(_ => new TCollection()).ToArray(); + + foreach (var collection in collections.Where(collection => collection.Count < keys.Length)) + foreach (var value in keys) + collection.TryAdd(value); + + if(collections.Any(collection => collection.Count != keys.Length)) + throw new InvalidOperationException(); + } + + internal static void FillStacks(ref Stack[] stacks, int collectionsCount, T[] keys) // Stack does not implement any interface that exposes .Push method + { + if (stacks == null) + stacks = Enumerable.Range(0, collectionsCount).Select(_ => new Stack()).ToArray(); + + foreach (var stack in stacks.Where(stack => stack.Count < keys.Length)) + foreach (var value in keys) + stack.Push(value); + + if(stacks.Any(collection => collection.Count != keys.Length)) + throw new InvalidOperationException(); + } + + internal static void FillQueues(ref Queue[] queues, int collectionsCount, T[] keys) // Queue does not implement any interface that exposes .Enqueue method + { + if (queues == null) + queues = Enumerable.Range(0, collectionsCount).Select(_ => new Queue()).ToArray(); + + foreach (var queue in queues.Where(queue => queue.Count < keys.Length)) + foreach (var value in keys) + queue.Enqueue(value); + + if(queues.Any(collection => collection.Count != keys.Length)) + throw new InvalidOperationException(); + } + } +} \ No newline at end of file From 6ccfc27927bb378943128fefb67a2aa7a1d10710 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Fri, 13 Jul 2018 21:30:31 +0200 Subject: [PATCH 19/31] add missing benchmarks --- .../corefx/System.Collections/Add/AddDefaultSize.cs | 10 ++++++++++ .../corefx/System.Collections/Add/AddGivenSize.cs | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs index 089434c8c9b..aed11a5b4c8 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs @@ -128,5 +128,15 @@ public ConcurrentStack ConcurrentStack() collection.Push(uniqueValues[i]); return collection; } + + [Benchmark] + public ConcurrentDictionary ConcurrentDictionary() + { + var collection = new ConcurrentDictionary(); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.TryAdd(uniqueValues[i], uniqueValues[i]); + return collection; + } } } \ No newline at end of file diff --git a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs index bd83b0abab5..93f170fa888 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections.Concurrent; +using System.Collections.Generic; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -79,5 +80,15 @@ public Stack Stack() collection.Push(uniqueValues[i]); return collection; } + + [Benchmark] + public ConcurrentDictionary ConcurrentDictionary() + { + var collection = new ConcurrentDictionary(Utils.ConcurrencyLevel, Count); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.TryAdd(uniqueValues[i], uniqueValues[i]); + return collection; + } } } \ No newline at end of file From 41aa067f0eebb734355221a3ab00a14dba1e522b Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 17 Jul 2018 16:36:22 +0200 Subject: [PATCH 20/31] fix a typo --- .../corefx/System.Collections/Create/CtorGivenSize.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs index 3ce81c972d6..5ad037e01e2 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs @@ -8,7 +8,7 @@ namespace System.Collections [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] [GenericTypeArguments(typeof(int))] // value type [GenericTypeArguments(typeof(string))] // reference type - public class CtorGiventSize + public class CtorGivenSize { [Params(Utils.DefaultCollectionSize)] public int Size; From 255f1537ad465c91ca6cfebe3877551c3e7d25e4 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 17 Jul 2018 16:46:34 +0200 Subject: [PATCH 21/31] add missing ConcurrentBag.TryTake benchmark --- .../corefx/System.Collections/Remove/Remove.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/benchmarks/corefx/System.Collections/Remove/Remove.cs b/src/benchmarks/corefx/System.Collections/Remove/Remove.cs index 096422d264b..db6baea09e4 100644 --- a/src/benchmarks/corefx/System.Collections/Remove/Remove.cs +++ b/src/benchmarks/corefx/System.Collections/Remove/Remove.cs @@ -33,6 +33,7 @@ public class Remove private Queue[] _queues; private ConcurrentStack[] _concurrentStacks; private ConcurrentQueue[] _concurrentQueues; + private ConcurrentBag[] _concurrentBags; [GlobalSetup] public void Setup() => _keys = UniqueValuesGenerator.GenerateArray(Size); @@ -183,5 +184,17 @@ public void ConcurrentQueue() foreach (var key in keys) // we don't need to iterate over keys but to compare apples to apples we do queue.TryDequeue(out _); } + + [IterationSetup(Target = nameof(ConcurrentBag))] + public void SetupConcurrentBagIteration() => Utils.FillProducerConsumerCollection(ref _concurrentBags, InvocationsPerIteration, _keys); + + [Benchmark] + public void ConcurrentBag() + { + var bag = _concurrentBags[_iterationIndex++]; + var keys = _keys; + foreach (var key in keys) // we don't need to iterate over keys but to compare apples to apples we do + bag.TryTake(out _); + } } } \ No newline at end of file From 843741ce746511ee8ea9eaef752c2af04d502df6 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 17 Jul 2018 17:23:08 +0200 Subject: [PATCH 22/31] AddRemoveFromSameThreads benchmarks --- .../Concurrent/AddRemoveFromSameThreads.cs | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Concurrent/AddRemoveFromSameThreads.cs diff --git a/src/benchmarks/corefx/System.Collections/Concurrent/AddRemoveFromSameThreads.cs b/src/benchmarks/corefx/System.Collections/Concurrent/AddRemoveFromSameThreads.cs new file mode 100644 index 00000000000..a01562ba48d --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Concurrent/AddRemoveFromSameThreads.cs @@ -0,0 +1,106 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using Benchmarks; + +namespace System.Collections.Concurrent +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + [MinWarmupCount(6, forceAutoWarmup: true)] + [MaxWarmupCount(10, forceAutoWarmup: true)] + public class AddRemoveFromSameThreads + { + const int NumThreads = 2; + + [Params(2_000_000)] + public int Size; + + private Barrier _barrier; + private Task[] _tasks; + + [IterationCleanup] + public void IterationCleanup() => _barrier.Dispose(); + + [IterationSetup(Target = nameof(ConcurrentBag))] + public void SetupConcurrentBagIteration() + { + var bag = new ConcurrentBag(); + + _barrier = new Barrier(NumThreads + 1); + _tasks = Enumerable.Range(0, NumThreads) + .Select(_ => + Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + + for (int i = 0; i < Size; i++) + { + bag.Add(default); + bag.TryTake(out T _); + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)) + .ToArray(); + } + + [Benchmark] + public void ConcurrentBag() => SignalAndWaitForAllTasks(); + + [IterationSetup(Target = nameof(ConcurrentStack))] + public void SetupConcurrentStackIteration() + { + var stack = new ConcurrentStack(); + + _barrier = new Barrier(NumThreads + 1); + _tasks = Enumerable.Range(0, NumThreads) + .Select(_ => + Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + + for (int i = 0; i < Size; i++) + { + stack.Push(default); + stack.TryPop(out T _); + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)) + .ToArray(); + } + + [Benchmark] + public void ConcurrentStack() => SignalAndWaitForAllTasks(); + + [IterationSetup(Target = nameof(ConcurrentQueue))] + public void SetupConcurrentQueueIteration() + { + var queue = new ConcurrentQueue(); + + _barrier = new Barrier(NumThreads + 1); + _tasks = Enumerable.Range(0, NumThreads) + .Select(_ => + Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + + for (int i = 0; i < Size; i++) + { + queue.Enqueue(default); + queue.TryDequeue(out T _); + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)) + .ToArray(); + } + + [Benchmark] + public void ConcurrentQueue() => SignalAndWaitForAllTasks(); + + private void SignalAndWaitForAllTasks() + { + _barrier.SignalAndWait(); + + Task.WaitAll(_tasks); + } + } +} \ No newline at end of file From baf6e768299941dc14a3bf0e592ffd4287c4cf5e Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 17 Jul 2018 18:50:26 +0200 Subject: [PATCH 23/31] AddRemoveFromDifferentThreads benchmarks --- .../AddRemoveFromDifferentThreads.cs | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Concurrent/AddRemoveFromDifferentThreads.cs diff --git a/src/benchmarks/corefx/System.Collections/Concurrent/AddRemoveFromDifferentThreads.cs b/src/benchmarks/corefx/System.Collections/Concurrent/AddRemoveFromDifferentThreads.cs new file mode 100644 index 00000000000..a670f8d653c --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Concurrent/AddRemoveFromDifferentThreads.cs @@ -0,0 +1,151 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using Benchmarks; + +namespace System.Collections.Concurrent +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + [MinWarmupCount(6, forceAutoWarmup: true)] + [MaxWarmupCount(10, forceAutoWarmup: true)] + public class AddRemoveFromDifferentThreads + { + const int NumThreads = 2; + + [Params(2_000_000)] + public int Size; + + private Barrier _barrier; + private Task _producer, _consumer; + + [IterationCleanup] + public void IterationCleanup() => _barrier.Dispose(); + + [IterationSetup(Target = nameof(ConcurrentBag))] + public void SetupConcurrentBagIteration() + { + var bag = new ConcurrentBag(); + + _barrier = new Barrier(NumThreads + 1); + + _producer = Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + _barrier.SignalAndWait(); + + for (int i = 0; i < Size; i++) + { + bag.Add(default); + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + _consumer = Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + _barrier.SignalAndWait(); + + int count = 0; + while (count < Size) + { + if (bag.TryTake(out T _)) + { + count++; + } + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + _barrier.SignalAndWait(); + } + + [Benchmark] + public void ConcurrentBag() => SignalAndWaitForAllTasks(); + + [IterationSetup(Target = nameof(ConcurrentStack))] + public void SetupConcurrentStackIteration() + { + var stack = new ConcurrentStack(); + + _barrier = new Barrier(NumThreads + 1); + + _producer = Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + _barrier.SignalAndWait(); + + for (int i = 0; i < Size; i++) + { + stack.Push(default); + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + _consumer = Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + _barrier.SignalAndWait(); + + int count = 0; + while (count < Size) + { + if (stack.TryPop(out T _)) + { + count++; + } + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + _barrier.SignalAndWait(); + } + + [Benchmark] + public void ConcurrentStack() => SignalAndWaitForAllTasks(); + + [IterationSetup(Target = nameof(ConcurrentQueue))] + public void SetupConcurrentQueueIteration() + { + var queue = new ConcurrentQueue(); + + _barrier = new Barrier(NumThreads + 1); + + _producer = Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + _barrier.SignalAndWait(); + + for (int i = 0; i < Size; i++) + { + queue.Enqueue(default); + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + _consumer = Task.Factory.StartNew(() => + { + _barrier.SignalAndWait(); + _barrier.SignalAndWait(); + + int count = 0; + while (count < Size) + { + if (queue.TryDequeue(out T _)) + { + count++; + } + } + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + _barrier.SignalAndWait(); + } + + [Benchmark] + public void ConcurrentQueue() => SignalAndWaitForAllTasks(); + + private void SignalAndWaitForAllTasks() + { + _barrier.SignalAndWait(); + + Task.WaitAll(_producer, _consumer); + } + } +} \ No newline at end of file From a99efaf1c2d564dd3ea98761023de5f1481c679a Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 17 Jul 2018 19:08:23 +0200 Subject: [PATCH 24/31] IsEmpty benchmarks --- .../System.Collections/Concurrent/IsEmpty.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Concurrent/IsEmpty.cs diff --git a/src/benchmarks/corefx/System.Collections/Concurrent/IsEmpty.cs b/src/benchmarks/corefx/System.Collections/Concurrent/IsEmpty.cs new file mode 100644 index 00000000000..dc202819fe1 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Concurrent/IsEmpty.cs @@ -0,0 +1,44 @@ +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections.Concurrent +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class IsEmpty + { + private ConcurrentDictionary _dictionary; + private ConcurrentQueue _queue; + private ConcurrentStack _stack; + private ConcurrentBag _bag; + + [Params(Utils.DefaultCollectionSize)] + public int Size; + + [GlobalSetup] + public void Setup() + { + var values = UniqueValuesGenerator.GenerateArray(Size); + + _dictionary = new ConcurrentDictionary(values.ToDictionary(v => v, v => v)); + _queue = new ConcurrentQueue(values); + _stack = new ConcurrentStack(values); + _bag = new ConcurrentBag(values); + } + + [Benchmark] + public bool Dictionary() => _dictionary.IsEmpty; + + [Benchmark] + public bool Queue() => _queue.IsEmpty; + + [Benchmark] + public bool Stack() => _stack.IsEmpty; + + [Benchmark] + public bool Bag() => _bag.IsEmpty; + } +} \ No newline at end of file From e279cfb158578e2afcc43b9167778409f81703e2 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 17 Jul 2018 19:09:05 +0200 Subject: [PATCH 25/31] Count benchmarks --- .../System.Collections/Concurrent/Count.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/benchmarks/corefx/System.Collections/Concurrent/Count.cs diff --git a/src/benchmarks/corefx/System.Collections/Concurrent/Count.cs b/src/benchmarks/corefx/System.Collections/Concurrent/Count.cs new file mode 100644 index 00000000000..e45c9ca3a61 --- /dev/null +++ b/src/benchmarks/corefx/System.Collections/Concurrent/Count.cs @@ -0,0 +1,44 @@ +using System.Linq; +using BenchmarkDotNet.Attributes; +using Benchmarks; +using Helpers; + +namespace System.Collections.Concurrent +{ + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] + [GenericTypeArguments(typeof(int))] // value type + [GenericTypeArguments(typeof(string))] // reference type + public class Count + { + private ConcurrentDictionary _dictionary; + private ConcurrentQueue _queue; + private ConcurrentStack _stack; + private ConcurrentBag _bag; + + [Params(Utils.DefaultCollectionSize)] + public int Size; + + [GlobalSetup] + public void Setup() + { + var values = UniqueValuesGenerator.GenerateArray(Size); + + _dictionary = new ConcurrentDictionary(values.ToDictionary(v => v, v => v)); + _queue = new ConcurrentQueue(values); + _stack = new ConcurrentStack(values); + _bag = new ConcurrentBag(values); + } + + [Benchmark] + public int Dictionary() => _dictionary.Count; + + [Benchmark] + public int Queue() => _queue.Count; + + [Benchmark] + public int Stack() => _stack.Count; + + [Benchmark] + public int Bag() => _bag.Count; + } +} \ No newline at end of file From 90fa210eee3465f7842073e3975e16f169b97040 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 17 Jul 2018 19:56:59 +0200 Subject: [PATCH 26/31] support new glob filters --- src/benchmarks/Program.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/benchmarks/Program.cs b/src/benchmarks/Program.cs index 7a1f35e7791..1191eb45046 100644 --- a/src/benchmarks/Program.cs +++ b/src/benchmarks/Program.cs @@ -61,12 +61,8 @@ private static IConfig GetConfig(Options options) config = config.With(new AllCategoriesFilter(options.AllCategories.ToArray())); if (options.AnyCategories.Any()) config = config.With(new AnyCategoriesFilter(options.AnyCategories.ToArray())); - if (options.Namespaces.Any()) - config = config.With(new NamespacesFilter(options.Namespaces.ToArray())); - if (options.MethodNames.Any()) - config = config.With(new MethodNamesFilter(options.MethodNames.ToArray())); - if (options.TypeNames.Any()) - config = config.With(new TypeNamesFilter(options.TypeNames.ToArray())); + if (options.Filters.Any()) + config = config.With(new GlobFilter(options.Filters.ToArray())); config = config.With(JsonExporter.Full); // make sure we export to Json (for BenchView integration purpose) @@ -282,14 +278,8 @@ public class Options [Option("anyCategories", Required = false, HelpText = "Any Categories to run")] public IEnumerable AnyCategories { get; set; } - [Option("namespace", Required = false, HelpText = "Namespace(s) to run")] - public IEnumerable Namespaces { get; set; } - - [Option("method", Required = false, HelpText = "Method(s) to run")] - public IEnumerable MethodNames { get; set; } - - [Option("class", Required = false, HelpText = "Class(es) with benchmarks to run")] - public IEnumerable TypeNames { get; set; } + [Option('f', "filters", Required = false, HelpText = "Filter(s) to apply, globs that operate on namespace.typename.methodname")] + public IEnumerable Filters { get; set; } [Option("join", Required = false, Default = false, HelpText = "Prints single table with results for all benchmarks")] public bool Join { get; set; } From 87527d1dfb0c387423450b3d882753d22ca28f72 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 19 Jul 2018 12:45:22 +0200 Subject: [PATCH 27/31] add ICollection, IDictionary and IEnumerable benchmarks --- src/benchmarks/Categories.cs | 1 + .../DefaultEqualityComparerPerf.cs | 2 +- .../System.Collections/Add/AddDefaultSize.cs | 27 +++++++++++ .../System.Collections/Add/AddGivenSize.cs | 45 +++++++++++++++---- .../Contains/ContainsFalse.cs | 15 +++++++ .../Contains/ContainsKeyFalse.cs | 15 +++++++ .../Contains/ContainsKeyTrue.cs | 15 +++++++ .../Contains/ContainsTrue.cs | 15 +++++++ .../System.Collections/Indexer/IndexerSet.cs | 13 ++++++ .../System.Collections/Iterate/IterateFor.cs | 18 ++++++++ .../Iterate/IterateForEach.cs | 18 ++++++++ .../System.Collections/Remove/Remove.cs | 15 ++++++- 12 files changed, 188 insertions(+), 11 deletions(-) diff --git a/src/benchmarks/Categories.cs b/src/benchmarks/Categories.cs index 870cf433321..d7d2b66e33e 100644 --- a/src/benchmarks/Categories.cs +++ b/src/benchmarks/Categories.cs @@ -10,6 +10,7 @@ public static class Categories public const string Inlining = "Inlining"; public const string V8 = "V8"; public const string Perflab = "Perflab"; + public const string Virtual = "Virtual"; public const string CoreFX = "CoreFX"; diff --git a/src/benchmarks/coreclr/Devirtualization/DefaultEqualityComparerPerf.cs b/src/benchmarks/coreclr/Devirtualization/DefaultEqualityComparerPerf.cs index 3fdfcff1f34..77d59d6393d 100644 --- a/src/benchmarks/coreclr/Devirtualization/DefaultEqualityComparerPerf.cs +++ b/src/benchmarks/coreclr/Devirtualization/DefaultEqualityComparerPerf.cs @@ -57,7 +57,7 @@ public bool CompareWrapped(ref T x, ref T y) } } - [BenchmarkCategory(Categories.CoreCLR)] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] public class EqualityComparer { public enum E diff --git a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs index aed11a5b4c8..d2768fe619f 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddDefaultSize.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -29,6 +30,19 @@ public List List() return collection; } + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public ICollection ICollection() => AddToICollection(new List()); + + [MethodImpl(MethodImplOptions.NoInlining)] // we want to prevent from inlining this particular method to make sure that JIT does not find out that ICollection is always List + private ICollection AddToICollection(ICollection collection) + { + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i]); + return collection; + } + [Benchmark] public HashSet HashSet() { @@ -49,6 +63,19 @@ public Dictionary Dictionary() return collection; } + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public IDictionary IDictionary() => AddToIDictionary(new Dictionary()); + + [MethodImpl(MethodImplOptions.NoInlining)] + private IDictionary AddToIDictionary(IDictionary collection) + { + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i], uniqueValues[i]); + return collection; + } + [Benchmark] public SortedList SortedList() { diff --git a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs index 93f170fa888..2d0436bbd2e 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -14,15 +15,28 @@ public class AddGivenSize private T[] _uniqueValues; [Params(Utils.DefaultCollectionSize)] - public int Count; + public int Size; [GlobalSetup] - public void Setup() => _uniqueValues = UniqueValuesGenerator.GenerateArray(Count); + public void Setup() => _uniqueValues = UniqueValuesGenerator.GenerateArray(Size); [Benchmark] public List List() { - var collection = new List(Count); + var collection = new List(Size); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i]); + return collection; + } + + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public ICollection ICollection() => AddToICollection(new List(Size)); + + [MethodImpl(MethodImplOptions.NoInlining)] + private ICollection AddToICollection(ICollection collection) + { var uniqueValues = _uniqueValues; for (int i = 0; i < uniqueValues.Length; i++) collection.Add(uniqueValues[i]); @@ -33,7 +47,7 @@ public List List() [Benchmark] public HashSet HashSet() { - var collection = new HashSet(Count); + var collection = new HashSet(Size); var uniqueValues = _uniqueValues; for(int i = 0; i < uniqueValues.Length; i++) collection.Add(uniqueValues[i]); @@ -44,7 +58,20 @@ public HashSet HashSet() [Benchmark] public Dictionary Dictionary() { - var collection = new Dictionary(Count); + var collection = new Dictionary(Size); + var uniqueValues = _uniqueValues; + for (int i = 0; i < uniqueValues.Length; i++) + collection.Add(uniqueValues[i], uniqueValues[i]); + return collection; + } + + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public IDictionary IDictionary() => AddToIDictionary(new Dictionary(Size)); + + [MethodImpl(MethodImplOptions.NoInlining)] + private IDictionary AddToIDictionary(IDictionary collection) + { var uniqueValues = _uniqueValues; for (int i = 0; i < uniqueValues.Length; i++) collection.Add(uniqueValues[i], uniqueValues[i]); @@ -54,7 +81,7 @@ public Dictionary Dictionary() [Benchmark] public SortedList SortedList() { - var collection = new SortedList(Count); + var collection = new SortedList(Size); var uniqueValues = _uniqueValues; for (int i = 0; i < uniqueValues.Length; i++) collection.Add(uniqueValues[i], uniqueValues[i]); @@ -64,7 +91,7 @@ public SortedList SortedList() [Benchmark] public Queue Queue() { - var collection = new Queue(Count); + var collection = new Queue(Size); var uniqueValues = _uniqueValues; for (int i = 0; i < uniqueValues.Length; i++) collection.Enqueue(uniqueValues[i]); @@ -74,7 +101,7 @@ public Queue Queue() [Benchmark] public Stack Stack() { - var collection = new Stack(Count); + var collection = new Stack(Size); var uniqueValues = _uniqueValues; for (int i = 0; i < uniqueValues.Length; i++) collection.Push(uniqueValues[i]); @@ -84,7 +111,7 @@ public Stack Stack() [Benchmark] public ConcurrentDictionary ConcurrentDictionary() { - var collection = new ConcurrentDictionary(Utils.ConcurrencyLevel, Count); + var collection = new ConcurrentDictionary(Utils.ConcurrencyLevel, Size); var uniqueValues = _uniqueValues; for (int i = 0; i < uniqueValues.Length; i++) collection.TryAdd(uniqueValues[i], uniqueValues[i]); diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs index 0531d3d1cef..9af028c8400 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -71,6 +72,20 @@ public bool List() return result; } + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public bool ICollection() => Contains(_list); + + [MethodImpl(MethodImplOptions.NoInlining)] + private bool Contains(ICollection collection) + { + bool result = default; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = collection.Contains(notFound[i]); + return result; + } + [Benchmark] public bool LinkedList() { diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs index 5d998e9c478..8c35d4cf7c0 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -52,6 +53,20 @@ public bool Dictionary() return result; } + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public bool IDictionary() => ContainsKey(_dictionary); + + [MethodImpl(MethodImplOptions.NoInlining)] + public bool ContainsKey(IDictionary collection) + { + bool result = default; + var notFound = _notFound; + for (int i = 0; i < notFound.Length; i++) + result = collection.ContainsKey(notFound[i]); + return result; + } + [Benchmark] public bool SortedList() { diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs index 115db1ef208..5982bbb8945 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -50,6 +51,20 @@ public bool Dictionary() return result; } + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public bool IDictionary() => ContainsKey(_dictionary); + + [MethodImpl(MethodImplOptions.NoInlining)] + public bool ContainsKey(IDictionary collection) + { + bool result = default; + var found = _found; + for (int i = 0; i < found.Length; i++) + result = collection.ContainsKey(found[i]); + return result; + } + [Benchmark] public bool SortedList() { diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs index 33d5e074297..329a8ed0dcc 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -68,6 +69,20 @@ public bool List() return result; } + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public bool ICollection() => Contains(_list); + + [MethodImpl(MethodImplOptions.NoInlining)] + private bool Contains(ICollection collection) + { + bool result = default; + var found = _found; + for (int i = 0; i < found.Length; i++) + result = collection.Contains(found[i]); + return result; + } + [Benchmark] public bool LinkedList() { diff --git a/src/benchmarks/corefx/System.Collections/Indexer/IndexerSet.cs b/src/benchmarks/corefx/System.Collections/Indexer/IndexerSet.cs index 06c8d49a835..2734a20f949 100644 --- a/src/benchmarks/corefx/System.Collections/Indexer/IndexerSet.cs +++ b/src/benchmarks/corefx/System.Collections/Indexer/IndexerSet.cs @@ -1,6 +1,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -55,6 +56,18 @@ public List List() return list; } + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public IList IList() => Set(_list); + + [MethodImpl(MethodImplOptions.NoInlining)] + private IList Set(IList collection) + { + for (int i = 0; i < collection.Count; i++) + collection[i] = default; + return collection; + } + [Benchmark] public Dictionary Dictionary() { diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs index 5edaef7472f..1ff343caa4b 100644 --- a/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateFor.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Collections.Immutable; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -16,6 +17,7 @@ public class IterateFor private T[] _array; private List _list; + private IList _ilist; private ImmutableArray _immutablearray; private ImmutableList _immutablelist; private ImmutableSortedSet _immutablesortedset; @@ -46,6 +48,22 @@ public T List() return result;; } + [GlobalSetup(Target = nameof(IList))] + public void SetupIList() => _ilist = new List(UniqueValuesGenerator.GenerateArray(Size)); + + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public T IList() => Get(_ilist); + + [MethodImpl(MethodImplOptions.NoInlining)] + private T Get(IList collection) + { + T result = default; + for (int i = 0; i < collection.Count; i++) + result = collection[i]; + return result; + } + [GlobalSetup(Target = nameof(ImmutableArray))] public void SetupImmutableArray() => _immutablearray = Immutable.ImmutableArray.CreateRange(UniqueValuesGenerator.GenerateArray(Size)); diff --git a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs index dd2e9a21360..818bf1e6d8b 100644 --- a/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs +++ b/src/benchmarks/corefx/System.Collections/Iterate/IterateForEach.cs @@ -1,6 +1,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -16,6 +17,7 @@ public class IterateForEach public int Size; private T[] _array; + private IEnumerable _ienumerable; private List _list; private LinkedList _linkedlist; private HashSet _hashset; @@ -51,6 +53,22 @@ public T Array() return result; } + [GlobalSetup(Target = nameof(IEnumerable))] + public void SetupIEnumerable() => _ienumerable = UniqueValuesGenerator.GenerateArray(Size); + + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public T IEnumerable() => Get(_ienumerable); + + [MethodImpl(MethodImplOptions.NoInlining)] + private T Get(IEnumerable collection) + { + T result = default; + foreach (var item in collection) + result = item; + return result; + } + [GlobalSetup(Target = nameof(List))] public void SetupList() => _list = new List(UniqueValuesGenerator.GenerateArray(Size)); diff --git a/src/benchmarks/corefx/System.Collections/Remove/Remove.cs b/src/benchmarks/corefx/System.Collections/Remove/Remove.cs index db6baea09e4..7d3245c0b46 100644 --- a/src/benchmarks/corefx/System.Collections/Remove/Remove.cs +++ b/src/benchmarks/corefx/System.Collections/Remove/Remove.cs @@ -1,6 +1,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using Benchmarks; using Helpers; @@ -41,7 +42,7 @@ public class Remove [IterationCleanup] public void CleanupIteration() => _iterationIndex = 0; // after every iteration end we set the index to 0 - [IterationSetup(Target = nameof(List))] + [IterationSetup(Targets = new []{ nameof(List), nameof(ICollection) })] public void SetupListIteration() => Utils.FillCollections(ref _lists, InvocationsPerIteration, _keys); [Benchmark] @@ -53,6 +54,18 @@ public void List() list.Remove(key); } + [Benchmark] + [BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)] + public void ICollection() => RemoveFromCollection(_lists[_iterationIndex++]); + + [MethodImpl(MethodImplOptions.NoInlining)] + private void RemoveFromCollection(ICollection collection) + { + var keys = _keys; + foreach (var key in keys) + collection.Remove(key); + } + [IterationSetup(Target = nameof(LinkedList))] public void SetupLinkedListIteration() => Utils.FillCollections(ref _linkedLists, InvocationsPerIteration, _keys); From 229c5e6bf8f983d42c861cf5dac80e9db42490f9 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 19 Jul 2018 12:54:22 +0200 Subject: [PATCH 28/31] use more future proof constant --- src/benchmarks/Benchmarks.csproj | 3 +++ src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs | 2 +- .../corefx/System.Collections/Add/TryAddDefaultSize.cs | 2 +- .../corefx/System.Collections/Add/TryAddGivenSize.cs | 2 +- .../corefx/System.Collections/Create/CtorGivenSize.cs | 2 +- src/benchmarks/corefx/System.Collections/Remove/Clear.cs | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/benchmarks/Benchmarks.csproj b/src/benchmarks/Benchmarks.csproj index 370f0682281..18a30003e9f 100644 --- a/src/benchmarks/Benchmarks.csproj +++ b/src/benchmarks/Benchmarks.csproj @@ -8,6 +8,9 @@ true latest + + $(DefineConstants);NETFRAMEWORK + diff --git a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs index 2d0436bbd2e..c9dad67f028 100644 --- a/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/AddGivenSize.cs @@ -43,7 +43,7 @@ private ICollection AddToICollection(ICollection collection) return collection; } -#if !NET461 // API added in .NET Core 2.0 +#if !NETFRAMEWORK // API added in .NET Core 2.0 [Benchmark] public HashSet HashSet() { diff --git a/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs index ff33589658b..5dafd160e6d 100644 --- a/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/TryAddDefaultSize.cs @@ -19,7 +19,7 @@ public class TryAddDefaultSize [GlobalSetup] public void Setup() => _uniqueValues = UniqueValuesGenerator.GenerateArray(Count); -#if !NET461 // API added in .NET Core 2.0 +#if !NETFRAMEWORK // API added in .NET Core 2.0 [Benchmark] public Dictionary Dictionary() { diff --git a/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs b/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs index 757447c2138..8ecdae098e0 100644 --- a/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Add/TryAddGivenSize.cs @@ -19,7 +19,7 @@ public class TryAddGiventSize [GlobalSetup] public void Setup() => _uniqueValues = UniqueValuesGenerator.GenerateArray(Count); -#if !NET461 // API added in .NET Core 2.0 +#if !NETFRAMEWORK // API added in .NET Core 2.0 [Benchmark] public Dictionary Dictionary() { diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs index 5ad037e01e2..311b388df62 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorGivenSize.cs @@ -19,7 +19,7 @@ public class CtorGivenSize [Benchmark] public List List() => new List(Size); -#if !NET461 // API added in .NET Core 2.0 +#if !NETFRAMEWORK // API added in .NET Core 2.0 [Benchmark] public HashSet HashSet() => new HashSet(Size); #endif diff --git a/src/benchmarks/corefx/System.Collections/Remove/Clear.cs b/src/benchmarks/corefx/System.Collections/Remove/Clear.cs index 657ea9318e0..b929d8e16c9 100644 --- a/src/benchmarks/corefx/System.Collections/Remove/Clear.cs +++ b/src/benchmarks/corefx/System.Collections/Remove/Clear.cs @@ -105,7 +105,7 @@ public class Clear [Benchmark] public void Queue() => _queues[_iterationIndex++].Clear(); -#if !NET461 // API added in .NET Core 2.0 +#if !NETFRAMEWORK // API added in .NET Core 2.0 [IterationSetup(Target = nameof(ConcurrentQueue))] public void SetupConcurrentQueueIteration() => Utils.FillProducerConsumerCollection(ref _concurrentQueues, InvocationsPerIteration, _keys); From d734b8e54b2c23b6dba984f5e17f007455749e50 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 19 Jul 2018 13:11:56 +0200 Subject: [PATCH 29/31] code review fixes --- .../corefx/Common/UniqueValuesGenerator.cs | 6 +- .../Contains/ContainsFalse.cs | 66 +++++++++---------- .../Contains/ContainsKeyFalse.cs | 44 ++++++------- .../Contains/ContainsKeyTrue.cs | 44 ++++++------- .../Contains/ContainsTrue.cs | 66 +++++++++---------- .../Create/CtorDefaultSize.cs | 2 +- 6 files changed, 115 insertions(+), 113 deletions(-) diff --git a/src/benchmarks/corefx/Common/UniqueValuesGenerator.cs b/src/benchmarks/corefx/Common/UniqueValuesGenerator.cs index 0ce2dcb6952..fcf006a8a47 100644 --- a/src/benchmarks/corefx/Common/UniqueValuesGenerator.cs +++ b/src/benchmarks/corefx/Common/UniqueValuesGenerator.cs @@ -6,9 +6,11 @@ namespace Helpers { internal static class UniqueValuesGenerator { + private const int Seed = 12345; // we always use the same seed to have repeatable results! + internal static T[] GenerateArray(int count) { - var random = new Random(12345); // we always use the same seed to have repeatable results! + var random = new Random(Seed); var uniqueValues = new HashSet(); @@ -25,7 +27,7 @@ internal static T[] GenerateArray(int count) internal static Dictionary GenerateDictionary(int count) { - var random = new Random(12345); // we always use the same seed to have repeatable results! + var random = new Random(Seed); var dictionary = new Dictionary(); diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs index 9af028c8400..7e78798d9a1 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsFalse.cs @@ -17,15 +17,15 @@ public class ContainsFalse private T[] _array; private List _list; - private LinkedList _linkedlist; - private HashSet _hashset; + private LinkedList _linkedList; + private HashSet _hashSet; private Queue _queue; private Stack _stack; - private SortedSet _sortedset; - private ImmutableArray _immutablearray; - private ImmutableHashSet _immutablehashset; - private ImmutableList _immutablelist; - private ImmutableSortedSet _immutablesortedset; + private SortedSet _sortedSet; + private ImmutableArray _immutableArray; + private ImmutableHashSet _immutableHashSet; + private ImmutableList _immutableList; + private ImmutableSortedSet _immutableSortedSet; [Params(Utils.DefaultCollectionSize)] public int Size; @@ -39,15 +39,15 @@ public void Setup() _array = secondHalf; _list = new List(secondHalf); - _linkedlist = new LinkedList(secondHalf); - _hashset = new HashSet(secondHalf); + _linkedList = new LinkedList(secondHalf); + _hashSet = new HashSet(secondHalf); _queue = new Queue(secondHalf); _stack = new Stack(secondHalf); - _sortedset = new SortedSet(secondHalf); - _immutablearray = Immutable.ImmutableArray.CreateRange(secondHalf); - _immutablehashset = Immutable.ImmutableHashSet.CreateRange(secondHalf); - _immutablelist = Immutable.ImmutableList.CreateRange(secondHalf); - _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(secondHalf); + _sortedSet = new SortedSet(secondHalf); + _immutableArray = Immutable.ImmutableArray.CreateRange(secondHalf); + _immutableHashSet = Immutable.ImmutableHashSet.CreateRange(secondHalf); + _immutableList = Immutable.ImmutableList.CreateRange(secondHalf); + _immutableSortedSet = Immutable.ImmutableSortedSet.CreateRange(secondHalf); } [Benchmark] @@ -57,7 +57,7 @@ public bool Array() var collection = _array; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -68,7 +68,7 @@ public bool List() var collection = _list; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -82,7 +82,7 @@ private bool Contains(ICollection collection) bool result = default; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -90,10 +90,10 @@ private bool Contains(ICollection collection) public bool LinkedList() { bool result = default; - var collection = _linkedlist; + var collection = _linkedList; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -101,10 +101,10 @@ public bool LinkedList() public bool HashSet() { bool result = default; - var collection = _hashset; + var collection = _hashSet; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -115,7 +115,7 @@ public bool Queue() var collection = _queue; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -126,7 +126,7 @@ public bool Stack() var collection = _stack; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -134,10 +134,10 @@ public bool Stack() public bool SortedSet() { bool result = default; - var collection = _sortedset; + var collection = _sortedSet; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -145,10 +145,10 @@ public bool SortedSet() public bool ImmutableArray() { bool result = default; - var collection = _immutablearray; + var collection = _immutableArray; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -156,10 +156,10 @@ public bool ImmutableArray() public bool ImmutableHashSet() { bool result = default; - var collection = _immutablehashset; + var collection = _immutableHashSet; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -167,10 +167,10 @@ public bool ImmutableHashSet() public bool ImmutableList() { bool result = default; - var collection = _immutablelist; + var collection = _immutableList; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } @@ -178,10 +178,10 @@ public bool ImmutableList() public bool ImmutableSortedSet() { bool result = default; - var collection = _immutablesortedset; + var collection = _immutableSortedSet; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.Contains(notFound[i]); + result ^= collection.Contains(notFound[i]); return result; } } diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs index 8c35d4cf7c0..c7bff8d14e9 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyFalse.cs @@ -18,11 +18,11 @@ public class ContainsKeyFalse private Dictionary _source; private Dictionary _dictionary; - private SortedList _sortedlist; - private SortedDictionary _sorteddictionary; - private ConcurrentDictionary _concurrentdictionary; - private ImmutableDictionary _immutabledictionary; - private ImmutableSortedDictionary _immutablesorteddictionary; + private SortedList _sortedList; + private SortedDictionary _sortedDictionary; + private ConcurrentDictionary _concurrentDictionary; + private ImmutableDictionary _immutableDictionary; + private ImmutableSortedDictionary _immutableSortedDictionary; [Params(Utils.DefaultCollectionSize)] public int Size; @@ -35,11 +35,11 @@ public void Setup() _source = values.Skip(Size).Take(Size).ToDictionary(item => item, item => (TValue)(object)item); _dictionary = new Dictionary(_source); - _sortedlist = new SortedList(_source); - _sorteddictionary = new SortedDictionary(_source); - _concurrentdictionary = new ConcurrentDictionary(_source); - _immutabledictionary = Immutable.ImmutableDictionary.CreateRange(_source); - _immutablesorteddictionary = Immutable.ImmutableSortedDictionary.CreateRange(_source); + _sortedList = new SortedList(_source); + _sortedDictionary = new SortedDictionary(_source); + _concurrentDictionary = new ConcurrentDictionary(_source); + _immutableDictionary = Immutable.ImmutableDictionary.CreateRange(_source); + _immutableSortedDictionary = Immutable.ImmutableSortedDictionary.CreateRange(_source); } [Benchmark] @@ -49,7 +49,7 @@ public bool Dictionary() var collection = _dictionary; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.ContainsKey(notFound[i]); + result ^= collection.ContainsKey(notFound[i]); return result; } @@ -63,7 +63,7 @@ public bool ContainsKey(IDictionary collection) bool result = default; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.ContainsKey(notFound[i]); + result ^= collection.ContainsKey(notFound[i]); return result; } @@ -71,10 +71,10 @@ public bool ContainsKey(IDictionary collection) public bool SortedList() { bool result = default; - var collection = _sortedlist; + var collection = _sortedList; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.ContainsKey(notFound[i]); + result ^= collection.ContainsKey(notFound[i]); return result; } @@ -82,10 +82,10 @@ public bool SortedList() public bool SortedDictionary() { bool result = default; - var collection = _sorteddictionary; + var collection = _sortedDictionary; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.ContainsKey(notFound[i]); + result ^= collection.ContainsKey(notFound[i]); return result; } @@ -93,10 +93,10 @@ public bool SortedDictionary() public bool ConcurrentDictionary() { bool result = default; - var collection = _concurrentdictionary; + var collection = _concurrentDictionary; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.ContainsKey(notFound[i]); + result ^= collection.ContainsKey(notFound[i]); return result; } @@ -104,10 +104,10 @@ public bool ConcurrentDictionary() public bool ImmutableDictionary() { bool result = default; - var collection = _immutabledictionary; + var collection = _immutableDictionary; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.ContainsKey(notFound[i]); + result ^= collection.ContainsKey(notFound[i]); return result; } @@ -115,10 +115,10 @@ public bool ImmutableDictionary() public bool ImmutableSortedDictionary() { bool result = default; - var collection = _immutablesorteddictionary; + var collection = _immutableSortedDictionary; var notFound = _notFound; for (int i = 0; i < notFound.Length; i++) - result = collection.ContainsKey(notFound[i]); + result ^= collection.ContainsKey(notFound[i]); return result; } } diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs index 5982bbb8945..6f580767c00 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsKeyTrue.cs @@ -18,11 +18,11 @@ public class ContainsKeyTrue private Dictionary _source; private Dictionary _dictionary; - private SortedList _sortedlist; - private SortedDictionary _sorteddictionary; - private ConcurrentDictionary _concurrentdictionary; - private ImmutableDictionary _immutabledictionary; - private ImmutableSortedDictionary _immutablesorteddictionary; + private SortedList _sortedList; + private SortedDictionary _sortedDictionary; + private ConcurrentDictionary _concurrentDictionary; + private ImmutableDictionary _immutableDictionary; + private ImmutableSortedDictionary _immutableSortedDictionary; [Params(Utils.DefaultCollectionSize)] public int Size; @@ -33,11 +33,11 @@ public void Setup() _found = UniqueValuesGenerator.GenerateArray(Size); _source = _found.ToDictionary(item => item, item => (TValue)(object)item); _dictionary = new Dictionary(_source); - _sortedlist = new SortedList(_source); - _sorteddictionary = new SortedDictionary(_source); - _concurrentdictionary = new ConcurrentDictionary(_source); - _immutabledictionary = Immutable.ImmutableDictionary.CreateRange(_source); - _immutablesorteddictionary = Immutable.ImmutableSortedDictionary.CreateRange(_source); + _sortedList = new SortedList(_source); + _sortedDictionary = new SortedDictionary(_source); + _concurrentDictionary = new ConcurrentDictionary(_source); + _immutableDictionary = Immutable.ImmutableDictionary.CreateRange(_source); + _immutableSortedDictionary = Immutable.ImmutableSortedDictionary.CreateRange(_source); } [Benchmark] @@ -47,7 +47,7 @@ public bool Dictionary() var collection = _dictionary; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.ContainsKey(found[i]); + result ^= collection.ContainsKey(found[i]); return result; } @@ -61,7 +61,7 @@ public bool ContainsKey(IDictionary collection) bool result = default; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.ContainsKey(found[i]); + result ^= collection.ContainsKey(found[i]); return result; } @@ -69,10 +69,10 @@ public bool ContainsKey(IDictionary collection) public bool SortedList() { bool result = default; - var collection = _sortedlist; + var collection = _sortedList; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.ContainsKey(found[i]); + result ^= collection.ContainsKey(found[i]); return result; } @@ -80,10 +80,10 @@ public bool SortedList() public bool SortedDictionary() { bool result = default; - var collection = _sorteddictionary; + var collection = _sortedDictionary; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.ContainsKey(found[i]); + result ^= collection.ContainsKey(found[i]); return result; } @@ -91,10 +91,10 @@ public bool SortedDictionary() public bool ConcurrentDictionary() { bool result = default; - var collection = _concurrentdictionary; + var collection = _concurrentDictionary; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.ContainsKey(found[i]); + result ^= collection.ContainsKey(found[i]); return result; } @@ -102,10 +102,10 @@ public bool ConcurrentDictionary() public bool ImmutableDictionary() { bool result = default; - var collection = _immutabledictionary; + var collection = _immutableDictionary; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.ContainsKey(found[i]); + result ^= collection.ContainsKey(found[i]); return result; } @@ -113,10 +113,10 @@ public bool ImmutableDictionary() public bool ImmutableSortedDictionary() { bool result = default; - var collection = _immutablesorteddictionary; + var collection = _immutableSortedDictionary; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.ContainsKey(found[i]); + result ^= collection.ContainsKey(found[i]); return result; } } diff --git a/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs b/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs index 329a8ed0dcc..53bd8dd1f21 100644 --- a/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs +++ b/src/benchmarks/corefx/System.Collections/Contains/ContainsTrue.cs @@ -17,15 +17,15 @@ public class ContainsTrue private T[] _array; private List _list; - private LinkedList _linkedlist; - private HashSet _hashset; + private LinkedList _linkedList; + private HashSet _hashSet; private Queue _queue; private Stack _stack; - private SortedSet _sortedset; - private ImmutableArray _immutablearray; - private ImmutableHashSet _immutablehashset; - private ImmutableList _immutablelist; - private ImmutableSortedSet _immutablesortedset; + private SortedSet _sortedSet; + private ImmutableArray _immutableArray; + private ImmutableHashSet _immutableHashSet; + private ImmutableList _immutableList; + private ImmutableSortedSet _immutableSortedSet; [Params(Utils.DefaultCollectionSize)] public int Size; @@ -36,15 +36,15 @@ public void Setup() _found = UniqueValuesGenerator.GenerateArray(Size); _array = _found.ToArray(); _list = new List(_found); - _linkedlist = new LinkedList(_found); - _hashset = new HashSet(_found); + _linkedList = new LinkedList(_found); + _hashSet = new HashSet(_found); _queue = new Queue(_found); _stack = new Stack(_found); - _sortedset = new SortedSet(_found); - _immutablearray = Immutable.ImmutableArray.CreateRange(_found); - _immutablehashset = Immutable.ImmutableHashSet.CreateRange(_found); - _immutablelist = Immutable.ImmutableList.CreateRange(_found); - _immutablesortedset = Immutable.ImmutableSortedSet.CreateRange(_found); + _sortedSet = new SortedSet(_found); + _immutableArray = Immutable.ImmutableArray.CreateRange(_found); + _immutableHashSet = Immutable.ImmutableHashSet.CreateRange(_found); + _immutableList = Immutable.ImmutableList.CreateRange(_found); + _immutableSortedSet = Immutable.ImmutableSortedSet.CreateRange(_found); } [Benchmark] @@ -54,7 +54,7 @@ public bool Array() var collection = _array; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -65,7 +65,7 @@ public bool List() var collection = _list; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -79,7 +79,7 @@ private bool Contains(ICollection collection) bool result = default; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -87,10 +87,10 @@ private bool Contains(ICollection collection) public bool LinkedList() { bool result = default; - var collection = _linkedlist; + var collection = _linkedList; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -98,10 +98,10 @@ public bool LinkedList() public bool HashSet() { bool result = default; - var collection = _hashset; + var collection = _hashSet; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -112,7 +112,7 @@ public bool Queue() var collection = _queue; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -123,7 +123,7 @@ public bool Stack() var collection = _stack; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -131,10 +131,10 @@ public bool Stack() public bool SortedSet() { bool result = default; - var collection = _sortedset; + var collection = _sortedSet; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -142,10 +142,10 @@ public bool SortedSet() public bool ImmutableArray() { bool result = default; - var collection = _immutablearray; + var collection = _immutableArray; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -153,10 +153,10 @@ public bool ImmutableArray() public bool ImmutableHashSet() { bool result = default; - var collection = _immutablehashset; + var collection = _immutableHashSet; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -164,10 +164,10 @@ public bool ImmutableHashSet() public bool ImmutableList() { bool result = default; - var collection = _immutablelist; + var collection = _immutableList; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } @@ -175,10 +175,10 @@ public bool ImmutableList() public bool ImmutableSortedSet() { bool result = default; - var collection = _immutablesortedset; + var collection = _immutableSortedSet; var found = _found; for (int i = 0; i < found.Length; i++) - result = collection.Contains(found[i]); + result ^= collection.Contains(found[i]); return result; } } diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs index 30f4a9f8fef..7eac19890d5 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs @@ -11,7 +11,7 @@ namespace System.Collections [GenericTypeArguments(typeof(string))] // reference type public class CtorDefaultSize { - [Benchmark] + [Benchmark] // we keep this benchmark to compare the cost of creating array vs cost of array wrappers (List, Queue, Stack) public T[] Array() => new T[4]; // array has no default size, List has = 4 so I decided to use 4 here [Benchmark] From 71d8e8e8ed6b91dbacfce904470ac52bdef002ab Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 19 Jul 2018 16:27:52 +0200 Subject: [PATCH 30/31] NETFRAMEWORK is predefined const! --- src/benchmarks/Benchmarks.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/benchmarks/Benchmarks.csproj b/src/benchmarks/Benchmarks.csproj index 18a30003e9f..370f0682281 100644 --- a/src/benchmarks/Benchmarks.csproj +++ b/src/benchmarks/Benchmarks.csproj @@ -8,9 +8,6 @@ true latest - - $(DefineConstants);NETFRAMEWORK - From fb87ea39b2efc6fce0585c078ae64691b33513b1 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 19 Jul 2018 16:30:46 +0200 Subject: [PATCH 31/31] remove the array benchmark --- .../corefx/System.Collections/Create/CtorDefaultSize.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs b/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs index 7eac19890d5..f5b60870a8c 100644 --- a/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs +++ b/src/benchmarks/corefx/System.Collections/Create/CtorDefaultSize.cs @@ -11,9 +11,6 @@ namespace System.Collections [GenericTypeArguments(typeof(string))] // reference type public class CtorDefaultSize { - [Benchmark] // we keep this benchmark to compare the cost of creating array vs cost of array wrappers (List, Queue, Stack) - public T[] Array() => new T[4]; // array has no default size, List has = 4 so I decided to use 4 here - [Benchmark] public List List() => new List();