Skip to content

Commit

Permalink
3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Jun 9, 2021
1 parent da19511 commit b00bc58
Show file tree
Hide file tree
Showing 27 changed files with 501 additions and 498 deletions.
11 changes: 2 additions & 9 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Features>strict</Features>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
16 changes: 9 additions & 7 deletions NetFabric.DoublyLinkedList.Benchmark/ConstructorBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;

namespace NetFabric.Benchmark
{
[MarkdownExporterAttribute.GitHub]
[MemoryDiagnoser]
public class ConstructorBenchmark
{
IEnumerable<int> enumerable;
IReadOnlyList<int> collection;
IEnumerable<int>? enumerable;
IReadOnlyList<int>? collection;

[Params(0, 10, 100)]
public int Count {get; set;}
Expand All @@ -23,22 +25,22 @@ public void GlobalSetup()

[Benchmark(Baseline = true)]
public LinkedList<int> LinkedList_Enumerable() =>
new LinkedList<int>(enumerable);
new(enumerable!);

[Benchmark]
public LinkedList<int> LinkedList_List() =>
new LinkedList<int>(collection);
new(collection!);

[Benchmark]
public DoublyLinkedList<int> DoublyLinkedList_Enumerable() =>
new DoublyLinkedList<int>(enumerable);
new(enumerable!);

[Benchmark]
public DoublyLinkedList<int> DoublyLinkedList_List() =>
new DoublyLinkedList<int>(collection, false);
new(collection!, reversed: false);

[Benchmark]
public DoublyLinkedList<int> DoublyLinkedList_List_Reversed() =>
new DoublyLinkedList<int>(collection, true);
new(collection!, reversed: true);
}
}
104 changes: 51 additions & 53 deletions NetFabric.DoublyLinkedList.Benchmark/EnumerationBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,108 @@
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;

namespace NetFabric.Benchmark
{
[MarkdownExporterAttribute.GitHub]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[CategoriesColumn]
[MemoryDiagnoser]
public class EnumerationBenchmark
{
const int count = 10_000;
LinkedList<int> linkedList;
DoublyLinkedList<int> doublyLinkedList;
LinkedList<int>? linkedList;
DoublyLinkedList<int>? doublyLinkedList;

[Params(100_000)]
public int Count { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
var data = Enumerable.Range(0, count);
var data = Enumerable.Range(0, Count);
linkedList = new LinkedList<int>(data);
doublyLinkedList = new DoublyLinkedList<int>(data);
}

[BenchmarkCategory("Forward")]
[Benchmark(Baseline = true)]
public int LinkedList_Forward_While()
{
var count = 0;
var current = linkedList.First;
while (current is object)
{
count += current.Value;
current = current.Next;
}
return count;
var sum = 0;
for (var current = linkedList!.First; current is not null; current = current.Next)
sum += current.Value;
return sum;
}

[BenchmarkCategory("Forward")]
[Benchmark]
public int LinkedList_Forward_ForEach()
{
var count = 0;
foreach (var value in linkedList)
count += value;
return count;
var sum = 0;
foreach (var value in linkedList!)
sum += value;
return sum;
}

[Benchmark]
[BenchmarkCategory("Reverse")]
[Benchmark(Baseline = true)]
public int LinkedList_Reverse_While()
{
var count = 0;
var current = linkedList.Last;
while (current is object)
{
count += current.Value;
current = current.Previous;
}
return count;
var sum = 0;
for (var current = linkedList!.Last; current is not null; current = current.Previous)
sum += current.Value;
return sum;
}

[BenchmarkCategory("Reverse")]
[Benchmark]
public int LinkedList_Reverse_ForEach()
{
var count = 0;
foreach (var value in linkedList.Reverse())
count += value;
return count;
var sum = 0;
foreach (var value in linkedList!.Reverse())
sum += value;
return sum;
}

[BenchmarkCategory("Forward")]
[Benchmark]
public int DoublyLinkedList_Forward_While()
{
var count = 0;
var current = doublyLinkedList.First;
while (current is object)
{
count += current.Value;
current = current.Next;
}
return count;
var sum = 0;
for (var current = doublyLinkedList!.First; current is not null; current = current.Next)
sum += current.Value;
return sum;
}

[BenchmarkCategory("Forward")]
[Benchmark]
public int DoublyLinkedList_Forward_ForEach()
{
var count = 0;
foreach (var value in doublyLinkedList.EnumerateForward())
count += value;
return count;
var sum = 0;
foreach (var value in doublyLinkedList!.Forward)
sum += value;
return sum;
}

[BenchmarkCategory("Reverse")]
[Benchmark]
public int DoublyLinkedList_Reverse_While()
{
var count = 0;
var current = doublyLinkedList.Last;
while (current is object)
{
count += current.Value;
current = current.Previous;
}
return count;
var sum = 0;
for (var current = doublyLinkedList!.Last; current is not null; current = current.Previous)
sum += current.Value;
return sum;
}

[BenchmarkCategory("Reverse")]
[Benchmark]
public int DoublyLinkedList_Reverse_ForEach()
{
var count = 0;
foreach (var value in doublyLinkedList.EnumerateReversed())
count += value;
return count;
var sum = 0;
foreach (var value in doublyLinkedList!.Backward)
sum += value;
return sum;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions NetFabric.DoublyLinkedList.Tests/AddAfterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void InvalidNode()
{
// Arrange
var list = new DoublyLinkedList<int>();
var anotherList = new DoublyLinkedList<int>(new int[] { 1 });
var anotherList = new DoublyLinkedList<int>(new[] { 1 });
var node = anotherList.Find(1);

// Act
Expand All @@ -41,10 +41,10 @@ void InvalidNode()
}

public static TheoryData<IReadOnlyList<int>, int, int, IReadOnlyList<int>> ItemData =>
new TheoryData<IReadOnlyList<int>, int, int, IReadOnlyList<int>>
new()
{
{ new int[] { 1 }, 1, 2, new int[] { 1, 2 } },
{ new int[] { 1, 3 }, 1, 2, new int[] { 1, 2, 3 } },
{ new[] { 1 }, 1, 2, new[] { 1, 2 } },
{ new[] { 1, 3 }, 1, 2, new[] { 1, 2, 3 } },
};

[Theory]
Expand All @@ -62,10 +62,10 @@ void AddItem(IReadOnlyList<int> collection, int after, int item, IReadOnlyList<i
// Assert
list.Version.Must()
.BeNotEqualTo(version);
list.EnumerateForward().Must()
list.Forward.Must()
.BeEnumerableOf<int>()
.BeEqualTo(expected);
list.EnumerateReversed().Must()
list.Backward.Must()
.BeEnumerableOf<int>()
.BeEqualTo(expected.Reverse());
}
Expand Down
12 changes: 6 additions & 6 deletions NetFabric.DoublyLinkedList.Tests/AddBeforeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void InvalidNode()
{
// Arrange
var list = new DoublyLinkedList<int>();
var anotherList = new DoublyLinkedList<int>(new int[] { 1 });
var anotherList = new DoublyLinkedList<int>(new[] { 1 });
var node = anotherList.Find(1);

// Act
Expand All @@ -41,10 +41,10 @@ void InvalidNode()
}

public static TheoryData<IReadOnlyList<int>, int, int, IReadOnlyList<int>> ItemData =>
new TheoryData<IReadOnlyList<int>, int, int, IReadOnlyList<int>>
new()
{
{ new int[] { 2 }, 2, 1, new int[] { 1, 2 } },
{ new int[] { 1, 3 }, 3, 2, new int[] { 1, 2, 3 } },
{ new[] { 2 }, 2, 1, new[] { 1, 2 } },
{ new[] { 1, 3 }, 3, 2, new[] { 1, 2, 3 } },
};

[Theory]
Expand All @@ -62,10 +62,10 @@ void AddItem(IReadOnlyList<int> collection, int after, int item, IReadOnlyList<i
// Assert
list.Version.Must()
.BeNotEqualTo(version);
list.EnumerateForward().Must()
list.Forward.Must()
.BeEnumerableOf<int>()
.BeEqualTo(expected);
list.EnumerateReversed().Must()
list.Backward.Must()
.BeEnumerableOf<int>()
.BeEqualTo(expected.Reverse());
}
Expand Down
Loading

0 comments on commit b00bc58

Please sign in to comment.