-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PriorityQueue benchmarks (#1665)
- Loading branch information
1 parent
6e941f7
commit bcf9863
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/benchmarks/micro/libraries/System.Collections/PriorityQueue/Perf_PriorityQueue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Extensions; | ||
using MicroBenchmarks; | ||
|
||
namespace System.Collections.Tests | ||
{ | ||
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)] | ||
[GenericTypeArguments(typeof(int), typeof(int))] | ||
[GenericTypeArguments(typeof(string), typeof(string))] | ||
[GenericTypeArguments(typeof(Guid), typeof(Guid))] | ||
public class Perf_PriorityQueue<TElement, TPriority> | ||
{ | ||
[Params(10, 100, 1000)] | ||
public int Size; | ||
|
||
private (TElement Element, TPriority Priority)[] _items; | ||
private PriorityQueue<TElement, TPriority> _priorityQueue; | ||
private PriorityQueue<TElement, TPriority> _prePopulatedPriorityQueue; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_items = ValuesGenerator.Array<TElement>(Size).Zip(ValuesGenerator.Array<TPriority>(Size)).ToArray(); | ||
_priorityQueue = new PriorityQueue<TElement, TPriority>(initialCapacity: Size); | ||
_prePopulatedPriorityQueue = new PriorityQueue<TElement, TPriority>(_items); | ||
} | ||
|
||
[Benchmark] | ||
public void HeapSort() | ||
{ | ||
var queue = _priorityQueue; | ||
queue.EnqueueRange(_items); | ||
|
||
while (queue.Count > 0) | ||
{ | ||
queue.Dequeue(); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Enumerate() | ||
{ | ||
foreach (var _ in _prePopulatedPriorityQueue.UnorderedItems) | ||
{ | ||
|
||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Dequeue_And_Enqueue() | ||
{ | ||
// benchmarks dequeue and enqueue operations | ||
// for heaps of fixed size. | ||
|
||
var queue = _priorityQueue; | ||
var items = _items; | ||
|
||
// populate the heap: incorporated in the | ||
// benchmark to achieve determinism. | ||
foreach ((TElement element, TPriority priority) in items) | ||
{ | ||
queue.Enqueue(element, priority); | ||
} | ||
|
||
foreach ((TElement element, TPriority priority) in items) | ||
{ | ||
queue.Dequeue(); | ||
queue.Enqueue(element, priority); | ||
} | ||
|
||
// Drain the heap | ||
while (queue.Count > 0) | ||
{ | ||
queue.Dequeue(); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void K_Max_Elements() | ||
{ | ||
const int k = 5; | ||
var queue = _priorityQueue; | ||
var items = _items; | ||
|
||
for (int i = 0; i < k; i++) | ||
{ | ||
(TElement element, TPriority priority) = items[i]; | ||
queue.Enqueue(element, priority); | ||
} | ||
|
||
for (int i = k; i < Size; i++) | ||
{ | ||
(TElement element, TPriority priority) = items[i]; | ||
queue.EnqueueDequeue(element, priority); | ||
} | ||
|
||
for (int i = 0; i < k; i++) | ||
{ | ||
queue.Dequeue(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters