From c10289c46c59d3772a57d7da1e244f9893286456 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 18 Oct 2026 14:20:34 +0200 Subject: [PATCH] Populate array with same value: .NET 10, span fill, BenchmarkDotNet args Moves all three projects to net10.0 with BenchmarkDotNet 0.15.8, MSTest 4.5.1, Microsoft.NET.Test.Sdk 18.11.0 and coverlet.collector 10.1.0. The benchmark gets [MemoryDiagnoser] and Program.cs now passes args to BenchmarkRunner, so --job and --filter work. InstantiateArrayManually uses a collection expression, and FillWithSpan and FillRangeWithSpan show the AsSpan().Fill() route with two new tests. --- .../ArrayPopulatorBenchmark.cs | 58 +++--- .../BenchmarkRunner/BenchmarkRunner.csproj | 4 +- .../BenchmarkRunner/Program.cs | 15 +- .../ArrayPopulator.cs | 101 +++++---- .../PopulateArrayWithTheSameValue.csproj | 3 +- .../Tests/Tests.cs | 191 ++++++++++-------- .../Tests/Tests.csproj | 10 +- 7 files changed, 211 insertions(+), 171 deletions(-) diff --git a/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/ArrayPopulatorBenchmark.cs b/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/ArrayPopulatorBenchmark.cs index 13f6f7b085..652e035341 100644 --- a/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/ArrayPopulatorBenchmark.cs +++ b/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/ArrayPopulatorBenchmark.cs @@ -1,40 +1,40 @@ using BenchmarkDotNet.Attributes; using PopulateArrayWithTheSameValue; -namespace BenchmarkRunner +namespace BenchmarkRunner; + +[MemoryDiagnoser] +public class ArrayPopulatorBenchmark { - public class ArrayPopulatorBenchmark + private static readonly ArrayPopulator _arrayPopulator = new(); + private readonly Article[] _benchMarkArray = new Article[100_000]; + private readonly Article _benchmarkArticle = new() { - private static readonly ArrayPopulator _arrayPopulator = new(); - private readonly Article[] _benchMarkArray = new Article[1000 * 100]; - private readonly Article _benchmarkArticle = new() - { - Title = "How to Copy Array Elements to New Array in C#", - LastUpdate = new DateTime(2022, 01, 31) - }; + Title = "How to Copy Array Elements to New Array in C#", + LastUpdate = new DateTime(2022, 01, 31) + }; - [Benchmark] - public void FillArray() - { - _arrayPopulator.FillArray(_benchMarkArray, _benchmarkArticle); - } + [Benchmark] + public void FillArray() + { + _arrayPopulator.FillArray(_benchMarkArray, _benchmarkArticle); + } - [Benchmark] - public void EnumerableRepeat() - { - _arrayPopulator.EnumerableRepeat(_benchmarkArticle, _benchMarkArray.Length); - } + [Benchmark] + public void EnumerableRepeat() + { + _arrayPopulator.EnumerableRepeat(_benchmarkArticle, _benchMarkArray.Length); + } - [Benchmark] - public void ForStatement() - { - _arrayPopulator.ForStatement(_benchMarkArray, _benchmarkArticle); - } + [Benchmark] + public void ForStatement() + { + _arrayPopulator.ForStatement(_benchMarkArray, _benchmarkArticle); + } - [Benchmark] - public void ForStatementShallowCopy() - { - _arrayPopulator.ForStatementShallowCopy(_benchMarkArray, _benchmarkArticle); - } + [Benchmark] + public void ForStatementShallowCopy() + { + _arrayPopulator.ForStatementShallowCopy(_benchMarkArray, _benchmarkArticle); } } diff --git a/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/BenchmarkRunner.csproj b/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/BenchmarkRunner.csproj index 71d23611c5..48160ee386 100644 --- a/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/BenchmarkRunner.csproj +++ b/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/BenchmarkRunner.csproj @@ -2,13 +2,13 @@ Exe - net6.0 + net10.0 enable enable - + diff --git a/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/Program.cs b/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/Program.cs index 4493f82280..dd1c8d7d88 100644 --- a/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/Program.cs +++ b/collections-arrays/PopulateArrayWithTheSameValue/BenchmarkRunner/Program.cs @@ -1,10 +1,11 @@ -namespace BenchmarkRunner +using BenchmarkDotNet.Running; + +namespace BenchmarkRunner; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - BenchmarkDotNet.Running.BenchmarkRunner.Run(); - } + BenchmarkDotNet.Running.BenchmarkRunner.Run(args: args); } -} \ No newline at end of file +} diff --git a/collections-arrays/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue/ArrayPopulator.cs b/collections-arrays/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue/ArrayPopulator.cs index aaf7462016..3122ea91d6 100644 --- a/collections-arrays/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue/ArrayPopulator.cs +++ b/collections-arrays/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue/ArrayPopulator.cs @@ -1,59 +1,74 @@ -namespace PopulateArrayWithTheSameValue +namespace PopulateArrayWithTheSameValue; + +public class ArrayPopulator { - public class ArrayPopulator + public Article[] InstantiateArrayManually() { - public Article[] InstantiateArrayManually() - { - return new Article[] - { - new Article { Title = "How to Copy Array Elements to New Array in C#", LastUpdate = new DateTime(2022, 01, 31)}, - new Article { Title = "How to Copy Array Elements to New Array in C#", LastUpdate = new DateTime(2022, 01, 31)}, - }; - } + return + [ + new() { Title = "How to Copy Array Elements to New Array in C#", LastUpdate = new DateTime(2022, 01, 31) }, + new() { Title = "How to Copy Array Elements to New Array in C#", LastUpdate = new DateTime(2022, 01, 31) } + ]; + } - public Article[] FillArray(Article[] array, Article value) - { - Array.Fill(array, value); + public Article[] FillArray(Article[] array, Article value) + { + Array.Fill(array, value); - return array; - } - - public Article[] FillArrayWithAditionalParameter(Article[] array, Article value) - { - Array.Fill(array, value, 5, 2); + return array; + } - return array; - } + public Article[] FillArrayWithAditionalParameter(Article[] array, Article value) + { + Array.Fill(array, value, 5, 2); - public Article[] EnumerableRepeat(Article value, int count) - { - var articles = Enumerable.Repeat(value, count).ToArray(); + return array; + } - return articles; - } + public Article[] FillWithSpan(Article article) + { + var articles = new Article[10]; + articles.AsSpan().Fill(article); + articles.AsSpan(5, 2).Fill(article); - public Article[] ForStatement(Article[] articles, Article article) + return articles; + } + + public Article[] FillRangeWithSpan(Article[] articles, Article article) + { + articles.AsSpan(5, 2).Fill(article); + + return articles; + } + + public Article[] EnumerableRepeat(Article value, int count) + { + var articles = Enumerable.Repeat(value, count).ToArray(); + + return articles; + } + + public Article[] ForStatement(Article[] articles, Article article) + { + for (int index = 0; index < articles.Length; index++) { - for (int index = 0; index < articles.Length; index++) + articles[index] = new Article { - articles[index] = new Article - { - Title = article.Title, - LastUpdate = article.LastUpdate - }; - } - - return articles; + Title = article.Title, + LastUpdate = article.LastUpdate + }; } - public Article[] ForStatementShallowCopy(Article[] articles, Article article) - { - for (int index = 0; index < articles.Length; index++) - { - articles[index] = article; - } + return articles; + } - return articles; + public Article[] ForStatementShallowCopy(Article[] articles, Article article) + { + for (int index = 0; index < articles.Length; index++) + { + articles[index] = article; } + + return articles; } } diff --git a/collections-arrays/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue.csproj b/collections-arrays/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue.csproj index e435c65134..3c2e30e3de 100644 --- a/collections-arrays/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue.csproj +++ b/collections-arrays/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue/PopulateArrayWithTheSameValue.csproj @@ -2,8 +2,7 @@ Exe - net6.0 - 10 + net10.0 enable enable diff --git a/collections-arrays/PopulateArrayWithTheSameValue/Tests/Tests.cs b/collections-arrays/PopulateArrayWithTheSameValue/Tests/Tests.cs index a6dc8f36c1..96abf8da42 100644 --- a/collections-arrays/PopulateArrayWithTheSameValue/Tests/Tests.cs +++ b/collections-arrays/PopulateArrayWithTheSameValue/Tests/Tests.cs @@ -3,107 +3,132 @@ using System; using System.Linq; -namespace Tests +namespace Tests; + +[TestClass] +public class Tests { - [TestClass] - public class Tests + private readonly ArrayPopulator _arrayPopulator = new(); + private readonly Article _article = new() + { + Title = "How to Find the Maximum Value of an Array in C#", + LastUpdate = new DateTime(2022, 02, 07) + }; + + [TestMethod] + public void GivenTheMainProgram_ThenRunTheMainMethodAndWriteResultsAtTheConsole() { - private readonly ArrayPopulator _arrayPopulator = new(); - private readonly Article _article = new() - { - Title = "How to Find the Maximum Value of an Array in C#", - LastUpdate = new DateTime(2022, 02, 07) - }; - - [TestMethod] - public void GivenTheMainProgram_ThenRunTheMainMethodAndWriteResultsAtTheConsole() - { - Program.Main(Array.Empty()); + Program.Main(Array.Empty()); - Assert.AreEqual(1, Program.OutputResult); - } + Assert.AreEqual(1, Program.OutputResult); + } - [TestMethod] - public void GivenTheMainProgram_ThenReturnAnEmptyArray() - { - var emptyArray = Program.InstantiateInitialArray(); + [TestMethod] + public void GivenTheMainProgram_ThenReturnAnEmptyArray() + { + var emptyArray = Program.InstantiateInitialArray(); - Assert.IsNotNull(emptyArray); - Assert.IsNull(emptyArray[0]); - } + Assert.IsNotNull(emptyArray); + Assert.IsNull(emptyArray[0]); + } - [TestMethod] - public void GivenTheMainProgram_ThenReturnANewArticle() - { - var article = Program.InstantiateNewArticle(); + [TestMethod] + public void GivenTheMainProgram_ThenReturnANewArticle() + { + var article = Program.InstantiateNewArticle(); - Assert.IsNotNull(article); - } + Assert.IsNotNull(article); + } - [TestMethod] - public void GivenTheArrayPopulator_ThenReturnANewPopulatedArrayManually() - { - var populatedArray = _arrayPopulator.InstantiateArrayManually(); + [TestMethod] + public void GivenTheArrayPopulator_ThenReturnANewPopulatedArrayManually() + { + var populatedArray = _arrayPopulator.InstantiateArrayManually(); - //It can't be null - Assert.IsNotNull(populatedArray); - //It has at least one element - Assert.IsTrue(populatedArray.Any()); - } + //It can't be null + Assert.IsNotNull(populatedArray); + //It has at least one element + Assert.IsTrue(populatedArray.Any()); + } - [TestMethod] - public void GivenAnEmptyInitialArrayAndAnObject_ThenReturnAFilledArrayUsingArrayFill() - { - var populatedArray = _arrayPopulator.FillArray(new Article[10], _article); + [TestMethod] + public void GivenAnEmptyInitialArrayAndAnObject_ThenReturnAFilledArrayUsingArrayFill() + { + var populatedArray = _arrayPopulator.FillArray(new Article[10], _article); - Assert.IsNotNull(populatedArray); - Assert.IsTrue(populatedArray.Any()); - Assert.AreEqual(populatedArray[0], _article); - } + Assert.IsNotNull(populatedArray); + Assert.IsTrue(populatedArray.Any()); + Assert.AreEqual(populatedArray[0], _article); + } - [TestMethod] - public void GivenAnInitialArrayAndAnObject_ThenReturnAnArrayFilledInIndexFiveAndSixUsingArrayFill() - { - var partiallyFilledArray = _arrayPopulator.FillArrayWithAditionalParameter(new Article[10], _article); - - Assert.IsNotNull(partiallyFilledArray); - Assert.IsTrue(partiallyFilledArray.Any()); - Assert.IsNull(partiallyFilledArray[0]); - Assert.IsNotNull(partiallyFilledArray[5]); - Assert.IsNotNull(partiallyFilledArray[6]); - Assert.AreEqual(partiallyFilledArray[5], _article); - Assert.AreEqual(partiallyFilledArray[6], _article); - } + [TestMethod] + public void GivenAnInitialArrayAndAnObject_ThenReturnAnArrayFilledInIndexFiveAndSixUsingArrayFill() + { + var partiallyFilledArray = _arrayPopulator.FillArrayWithAditionalParameter(new Article[10], _article); + + Assert.IsNotNull(partiallyFilledArray); + Assert.IsTrue(partiallyFilledArray.Any()); + Assert.IsNull(partiallyFilledArray[0]); + Assert.IsNotNull(partiallyFilledArray[5]); + Assert.IsNotNull(partiallyFilledArray[6]); + Assert.AreEqual(partiallyFilledArray[5], _article); + Assert.AreEqual(partiallyFilledArray[6], _article); + } - [TestMethod] - public void GivenAnObject_ThenReturnAnArrayFilledWithThisObjectUsingEnumerableRepeat() - { - var populatedArticle = _arrayPopulator.EnumerableRepeat(_article, 10); + [TestMethod] + public void GivenAnObject_ThenReturnAnArrayFilledWithThisObjectUsingEnumerableRepeat() + { + var populatedArticle = _arrayPopulator.EnumerableRepeat(_article, 10); - Assert.IsNotNull(populatedArticle); - Assert.IsTrue(populatedArticle.Any()); - } + Assert.IsNotNull(populatedArticle); + Assert.IsTrue(populatedArticle.Any()); + } + + [TestMethod] + public void GiveAnArticleAndAnObject_ThenReturnAFilledArrayUsingFor() + { + var populatedArticle = _arrayPopulator.ForStatement(new Article[10], _article); + + Assert.IsNotNull(populatedArticle); + Assert.IsTrue(populatedArticle.Any()); + Assert.AreNotSame(populatedArticle[0], _article); + Assert.AreNotSame(populatedArticle[0], populatedArticle[1]); + } - [TestMethod] - public void GiveAnArticleAndAnObject_ThenReturnAFilledArrayUsingFor() + [TestMethod] + public void GiveAnArticleAndAnObject_ThenReturnAFilledArrayUsingForWithShallowCopy() + { + var populatedArticle = _arrayPopulator.ForStatementShallowCopy(new Article[10], _article); + + Assert.IsNotNull(populatedArticle); + Assert.IsTrue(populatedArticle.Any()); + Assert.AreSame(populatedArticle[0], _article); + Assert.AreSame(populatedArticle[0], populatedArticle[1]); + } + + [TestMethod] + public void GivenAnObject_ThenReturnAnArrayFilledWithThisObjectUsingSpanFill() + { + var populatedArray = _arrayPopulator.FillWithSpan(_article); + + Assert.HasCount(10, populatedArray); + foreach (var item in populatedArray) { - var populatedArticle = _arrayPopulator.ForStatement(new Article[10], _article); - - Assert.IsNotNull(populatedArticle); - Assert.IsTrue(populatedArticle.Any()); - Assert.AreNotSame(populatedArticle[0], _article); - Assert.AreNotSame(populatedArticle[0], populatedArticle[1]); + Assert.AreSame(_article, item); } + } - [TestMethod] - public void GiveAnArticleAndAnObject_ThenReturnAFilledArrayUsingForWithShallowCopy() - { - var populatedArticle = _arrayPopulator.ForStatementShallowCopy(new Article[10], _article); + [TestMethod] + public void GivenAnInitialArrayAndAnObject_ThenReturnAnArrayFilledInIndexFiveAndSixUsingSpanFill() + { + var partiallyFilledArray = _arrayPopulator.FillRangeWithSpan(new Article[10], _article); - Assert.IsNotNull(populatedArticle); - Assert.IsTrue(populatedArticle.Any()); - Assert.AreSame(populatedArticle[0], _article); - Assert.AreSame(populatedArticle[0], populatedArticle[1]); + for (int index = 0; index < partiallyFilledArray.Length; index++) + { + if (index is 5 or 6) + Assert.AreSame(_article, partiallyFilledArray[index]); + else + Assert.IsNull(partiallyFilledArray[index]); } } -} \ No newline at end of file +} diff --git a/collections-arrays/PopulateArrayWithTheSameValue/Tests/Tests.csproj b/collections-arrays/PopulateArrayWithTheSameValue/Tests/Tests.csproj index 6c98a1a80e..5e72eb214a 100644 --- a/collections-arrays/PopulateArrayWithTheSameValue/Tests/Tests.csproj +++ b/collections-arrays/PopulateArrayWithTheSameValue/Tests/Tests.csproj @@ -1,17 +1,17 @@ - net6.0 + net10.0 enable false - - - - + + + +