Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ArrayPopulatorBenchmark>();
}
BenchmarkDotNet.Running.BenchmarkRunner.Run<ArrayPopulatorBenchmark>(args: args);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Loading
Loading