Skip to content

Commit 8d12917

Browse files
committed
Added immutable benchmark
1 parent 91b050b commit 8d12917

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed

ImmutablePerformance/AddBenchmark.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Immutable;
2+
using System.Collections.ObjectModel;
3+
using BenchmarkDotNet.Attributes;
4+
5+
[MemoryDiagnoser]
6+
public class AddBenchmark
7+
{
8+
private readonly ReadOnlyCollection<int> _readOnlyNumbers = Enumerable.Range(0, 1_000).ToList().AsReadOnly();
9+
private readonly ImmutableArray<int> _immutableArray = Enumerable.Range(0, 1_000).ToImmutableArray();
10+
private readonly ImmutableList<int> _immutableList = Enumerable.Range(0, 1_000).ToImmutableList();
11+
12+
private readonly int[] numbersToAdd = { 1, 2, 3, 4, 5, 6, 7, 8 };
13+
14+
[Benchmark]
15+
public IReadOnlyList<int> AddElementReadOnlyCollection()
16+
{
17+
var l = _readOnlyNumbers.ToList();
18+
l.AddRange(numbersToAdd);
19+
return l;
20+
}
21+
22+
[Benchmark]
23+
public ImmutableArray<int> AddElementImmutableArray()
24+
=> _immutableArray.AddRange(numbersToAdd);
25+
26+
[Benchmark]
27+
public ImmutableList<int> AddElementImmutableList()
28+
=> _immutableList.AddRange(numbersToAdd);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
12+
</ItemGroup>
13+
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImmutablePerformance", "ImmutablePerformance.csproj", "{CCA4AEAC-2B9E-43A4-8D94-4633B7F6B0E5}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{CCA4AEAC-2B9E-43A4-8D94-4633B7F6B0E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{CCA4AEAC-2B9E-43A4-8D94-4633B7F6B0E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{CCA4AEAC-2B9E-43A4-8D94-4633B7F6B0E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{CCA4AEAC-2B9E-43A4-8D94-4633B7F6B0E5}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Collections.Immutable;
2+
using System.Collections.ObjectModel;
3+
using BenchmarkDotNet.Attributes;
4+
5+
public class IterateBenchmark
6+
{
7+
private readonly List<int> _numbers = Enumerable.Range(0, 1_000).ToList();
8+
private readonly ReadOnlyCollection<int> _readOnlyNumbers = Enumerable.Range(0, 1_000).ToList().AsReadOnly();
9+
private readonly ImmutableArray<int> _immutableArray = Enumerable.Range(0, 1_000).ToImmutableArray();
10+
private readonly ImmutableList<int> _immutableList = Enumerable.Range(0, 1_000).ToImmutableList();
11+
12+
[Benchmark(Baseline = true)]
13+
public int IterateList()
14+
{
15+
var sum = 0;
16+
for (var i = 0; i < _numbers.Count; i++)
17+
{
18+
sum += _numbers[i];
19+
}
20+
21+
return sum;
22+
}
23+
24+
[Benchmark]
25+
public int IterateReadOnlyCollection()
26+
{
27+
var sum = 0;
28+
for (var i = 0; i < _readOnlyNumbers.Count; i++)
29+
{
30+
sum += _readOnlyNumbers[i];
31+
}
32+
33+
return sum;
34+
}
35+
36+
[Benchmark]
37+
public int IterateImmutableArray()
38+
{
39+
var sum = 0;
40+
for (var i = 0; i < _immutableArray.Length; i++)
41+
{
42+
sum += _immutableArray[i];
43+
}
44+
45+
return sum;
46+
}
47+
48+
[Benchmark]
49+
public int IterateImmutableList()
50+
{
51+
var sum = 0;
52+
for (var i = 0; i < _immutableList.Count; i++)
53+
{
54+
sum += _immutableList[i];
55+
}
56+
57+
return sum;
58+
}
59+
}

ImmutablePerformance/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using BenchmarkDotNet.Running;
2+
3+
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run();

ImmutablePerformance/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Performance (ReadOnly)List vs Immutable collection types
2+
3+
A bit back on LinkedIn, there was a discussion about read-only collection and immutability where this is not the point I want to discuss now, as I already covered that here: [*"ReadOnlyCollection is not an immutable collection"*](https://steven-giesel.com/blogPost/c20eb758-a611-4f98-9ddf-b9e2b83fcac9).
4+
5+
This post is just about the performance of those types compared to our baseline, the good old `List<T>`. It also explains why we see the results we see.
6+
7+
Found [here](https://steven-giesel.com/blogPost/3bd2cb74-58a4-42cd-90e3-6c3a8befe854)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| ---------------------------------------------------------------------------------------- | ------------ |
7+
| [Performance (ReadOnly)List vs Immutable collection types](ImmutablePerformance/) | 26.02.2023 |
78
| [Multi-Tenancy with RavenDB and ASP.NET Core](MultiTenantRavenDB/) | 05.02.2023 |
89
| [Easy Pagintion for Entity Framework in 3 steps](PaginationEF/) | 09.01.2023 |
910
| [Give your strings context with StringSyntaxAttribute](StringHightlighting/) | 01.01.2023 |

0 commit comments

Comments
 (0)