Skip to content

Commit da25196

Browse files
ArrayList vs List: add the boxing benchmark project
1 parent 993968d commit da25196

4 files changed

Lines changed: 151 additions & 0 deletions

File tree

‎collections-lists/ArrayListVsList/ArrayListVsList.sln‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,54 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArrayListVsList", "ArrayLis
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Benchmarks.csproj", "{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
15+
Debug|x64 = Debug|x64
16+
Debug|x86 = Debug|x86
1317
Release|Any CPU = Release|Any CPU
18+
Release|x64 = Release|x64
19+
Release|x86 = Release|x86
1420
EndGlobalSection
1521
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1622
{844DACC5-2552-4136-8EE4-EC215A721884}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1723
{844DACC5-2552-4136-8EE4-EC215A721884}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{844DACC5-2552-4136-8EE4-EC215A721884}.Debug|x64.ActiveCfg = Debug|Any CPU
25+
{844DACC5-2552-4136-8EE4-EC215A721884}.Debug|x64.Build.0 = Debug|Any CPU
26+
{844DACC5-2552-4136-8EE4-EC215A721884}.Debug|x86.ActiveCfg = Debug|Any CPU
27+
{844DACC5-2552-4136-8EE4-EC215A721884}.Debug|x86.Build.0 = Debug|Any CPU
1828
{844DACC5-2552-4136-8EE4-EC215A721884}.Release|Any CPU.ActiveCfg = Release|Any CPU
1929
{844DACC5-2552-4136-8EE4-EC215A721884}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{844DACC5-2552-4136-8EE4-EC215A721884}.Release|x64.ActiveCfg = Release|Any CPU
31+
{844DACC5-2552-4136-8EE4-EC215A721884}.Release|x64.Build.0 = Release|Any CPU
32+
{844DACC5-2552-4136-8EE4-EC215A721884}.Release|x86.ActiveCfg = Release|Any CPU
33+
{844DACC5-2552-4136-8EE4-EC215A721884}.Release|x86.Build.0 = Release|Any CPU
2034
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2135
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Debug|x64.ActiveCfg = Debug|Any CPU
37+
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Debug|x64.Build.0 = Debug|Any CPU
38+
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Debug|x86.ActiveCfg = Debug|Any CPU
39+
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Debug|x86.Build.0 = Debug|Any CPU
2240
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Release|Any CPU.ActiveCfg = Release|Any CPU
2341
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Release|x64.ActiveCfg = Release|Any CPU
43+
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Release|x64.Build.0 = Release|Any CPU
44+
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Release|x86.ActiveCfg = Release|Any CPU
45+
{4B4BE163-4AD2-4848-AAD5-AB2C0A5F4F95}.Release|x86.Build.0 = Release|Any CPU
46+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Debug|x64.ActiveCfg = Debug|Any CPU
49+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Debug|x64.Build.0 = Debug|Any CPU
50+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Debug|x86.ActiveCfg = Debug|Any CPU
51+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Debug|x86.Build.0 = Debug|Any CPU
52+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Release|x64.ActiveCfg = Release|Any CPU
55+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Release|x64.Build.0 = Release|Any CPU
56+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Release|x86.ActiveCfg = Release|Any CPU
57+
{FB7AABD4-ABF9-4D94-AD26-61A0DE6EA773}.Release|x86.Build.0 = Release|Any CPU
2458
EndGlobalSection
2559
GlobalSection(SolutionProperties) = preSolution
2660
HideSolutionNode = FALSE
Lines changed: 14 additions & 0 deletions
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>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.4" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System.Collections;
2+
using BenchmarkDotNet.Attributes;
3+
4+
namespace BoxingBench;
5+
6+
// Measures the one claim the article argues from mechanism: List is faster than ArrayList
7+
// because it does not box, so the effect is large for value types and small for reference types.
8+
// Both collections are created with the final capacity, so the only allocation difference left
9+
// is the boxing itself.
10+
[MemoryDiagnoser]
11+
public class BoxingBenchmark
12+
{
13+
private const int N = 1000;
14+
15+
private static readonly string[] Words = CreateWords();
16+
17+
private static string[] CreateWords()
18+
{
19+
var words = new string[N];
20+
for (var i = 0; i < N; i++)
21+
{
22+
words[i] = "item-" + i;
23+
}
24+
25+
return words;
26+
}
27+
28+
[Benchmark(Description = "ArrayList, 1000 int")]
29+
public int ArrayListOfInt()
30+
{
31+
var collection = new ArrayList(N);
32+
for (var i = 0; i < N; i++)
33+
{
34+
collection.Add(i);
35+
}
36+
37+
var sum = 0;
38+
foreach (var item in collection)
39+
{
40+
sum += (int)item!;
41+
}
42+
43+
return sum;
44+
}
45+
46+
[Benchmark(Description = "List, 1000 int")]
47+
public int ListOfInt()
48+
{
49+
var collection = new List<int>(N);
50+
for (var i = 0; i < N; i++)
51+
{
52+
collection.Add(i);
53+
}
54+
55+
var sum = 0;
56+
foreach (var item in collection)
57+
{
58+
sum += item;
59+
}
60+
61+
return sum;
62+
}
63+
64+
[Benchmark(Description = "ArrayList, 1000 string")]
65+
public int ArrayListOfString()
66+
{
67+
var collection = new ArrayList(N);
68+
for (var i = 0; i < N; i++)
69+
{
70+
collection.Add(Words[i]);
71+
}
72+
73+
var total = 0;
74+
foreach (var item in collection)
75+
{
76+
total += ((string)item!).Length;
77+
}
78+
79+
return total;
80+
}
81+
82+
[Benchmark(Description = "List, 1000 string")]
83+
public int ListOfString()
84+
{
85+
var collection = new List<string>(N);
86+
for (var i = 0; i < N; i++)
87+
{
88+
collection.Add(Words[i]);
89+
}
90+
91+
var total = 0;
92+
foreach (var item in collection)
93+
{
94+
total += item.Length;
95+
}
96+
97+
return total;
98+
}
99+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using BenchmarkDotNet.Running;
2+
using BoxingBench;
3+
4+
BenchmarkRunner.Run<BoxingBenchmark>();

0 commit comments

Comments
 (0)