Skip to content

Commit

Permalink
Try using .NET 5
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Oct 13, 2023
1 parent 0d29435 commit 5131f5f
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 395 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 5.0.x

- name: Restore dependencies
run: dotnet restore
Expand Down
57 changes: 29 additions & 28 deletions NetFabric.Hyperlinq.Analyzer.Benchmarks/HLQ001_AssignmentBoxing.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
using BenchmarkDotNet.Attributes;

namespace NetFabric.Hyperlinq.Analyzer.Benchmarks;

public class HLQ001_AssignmentBoxing
namespace NetFabric.Hyperlinq.Analyzer.Benchmarks
{
List<int>? list;
IEnumerable<int>? enumerable;
public class HLQ001_AssignmentBoxing
{
List<int>? list;
IEnumerable<int>? enumerable;

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

[GlobalSetup]
public void GlobalSetup()
{
list = System.Linq.Enumerable.Range(0, Count).ToList();
enumerable = list;
}
[GlobalSetup]
public void GlobalSetup()
{
list = System.Linq.Enumerable.Range(0, Count).ToList();
enumerable = list;
}

[Benchmark(Baseline = true)]
public int Enumerable()
{
var sum = 0;
foreach (var item in enumerable!)
sum += item;
return sum;
}
[Benchmark(Baseline = true)]
public int Enumerable()
{
var sum = 0;
foreach (var item in enumerable!)
sum += item;
return sum;
}

[Benchmark]
public int List()
{
var sum = 0;
foreach (var item in list!)
sum += item;
return sum;
[Benchmark]
public int List()
{
var sum = 0;
foreach (var item in list!)
sum += item;
return sum;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
using BenchmarkDotNet.Attributes;

namespace NetFabric.Hyperlinq.Analyzer.Benchmarks;

public class HLQ004_RefEnumerationVariable
namespace NetFabric.Hyperlinq.Analyzer.Benchmarks
{
public readonly record struct Person(string Name, int Age);
public class HLQ004_RefEnumerationVariable
{
public readonly record struct Person(string Name, int Age);

Person[]? people;
Person[]? people;

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

[GlobalSetup]
public void GlobalSetup()
{
people = Enumerable.Range(0, Count)
.Select(value => new Person(value.ToString(), value))
.ToArray();
}
[GlobalSetup]
public void GlobalSetup()
{
people = Enumerable.Range(0, Count)
.Select(value => new Person(value.ToString(), value))
.ToArray();
}

[Benchmark(Baseline = true)]
public Person? Copy()
{
var oldest = default(Person);
foreach (var person in people!.AsSpan())
[Benchmark(Baseline = true)]
public Person? Copy()
{
if (person.Age > oldest.Age)
oldest = person;
var oldest = default(Person);
foreach (var person in people!.AsSpan())
{
if (person.Age > oldest.Age)
oldest = person;
}
return oldest;
}
return oldest;
}

[Benchmark]
public Person Ref()
{
var oldest = default(Person);
foreach (ref var person in people!.AsSpan())
[Benchmark]
public Person Ref()
{
if (person.Age > oldest.Age)
oldest = person;
var oldest = default(Person);
foreach (ref var person in people!.AsSpan())
{
if (person.Age > oldest.Age)
oldest = person;
}
return oldest;
}
return oldest;
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;

namespace NetFabric.Hyperlinq.Analyzer.Benchmarks;

[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[CategoriesColumn]
public class HLQ005_AvoidSingleAnalyzer
namespace NetFabric.Hyperlinq.Analyzer.Benchmarks
{
int[]? bestCase;
int[]? worstCase;

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

[GlobalSetup]
public void GlobalSetup()
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[CategoriesColumn]
public class HLQ005_AvoidSingleAnalyzer
{
bestCase = Enumerable.Range(0, Count).ToArray();
worstCase = bestCase.Reverse().ToArray();
int[]? bestCase;
int[]? worstCase;

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

[GlobalSetup]
public void GlobalSetup()
{
bestCase = Enumerable.Range(0, Count).ToArray();
worstCase = bestCase.Reverse().ToArray();
}

[BenchmarkCategory("BestCase")]
[Benchmark(Baseline = true)]
public int BestCase_Single()
=> bestCase!.Single(Comparer);

[BenchmarkCategory("BestCase")]
[Benchmark]
public int BestCase_First()
=> bestCase!.First(Comparer);

[BenchmarkCategory("WorstCase")]
[Benchmark(Baseline = true)]
public int WorstCase_Single()
=> worstCase!.Single(Comparer);

[BenchmarkCategory("WorstCase")]
[Benchmark]
public int WorstCase_First()
=> worstCase!.First(Comparer);

static bool Comparer(int value)
=> value == 0;
}

[BenchmarkCategory("BestCase")]
[Benchmark(Baseline = true)]
public int BestCase_Single()
=> bestCase!.Single(Comparer);

[BenchmarkCategory("BestCase")]
[Benchmark]
public int BestCase_First()
=> bestCase!.First(Comparer);

[BenchmarkCategory("WorstCase")]
[Benchmark(Baseline = true)]
public int WorstCase_Single()
=> worstCase!.Single(Comparer);

[BenchmarkCategory("WorstCase")]
[Benchmark]
public int WorstCase_First()
=> worstCase!.First(Comparer);

static bool Comparer(int value)
=> value == 0;
}
Loading

0 comments on commit 5131f5f

Please sign in to comment.