-
Notifications
You must be signed in to change notification settings - Fork 280
Add PriorityQueue benchmarks #1665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adamsitnik
merged 7 commits into
dotnet:master
from
eiriktsarpalis:priorityqueue-benchmarks
Feb 23, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fc34082
add PriorityQueue benchmarks
eiriktsarpalis 8f2cc0f
add heapify and enumeration benchmarks
eiriktsarpalis c1f3492
extend benchmarks to reference types
eiriktsarpalis e2a4dfa
remove comments
eiriktsarpalis 85f7c60
address feedback
eiriktsarpalis 937f515
add PriorityQueue Guid benchmarks
eiriktsarpalis efce7c9
add ValuesGenerator change
eiriktsarpalis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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(); | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_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(); | ||
} | ||
} | ||
} | ||
} | ||
eiriktsarpalis marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.