A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.Collections.Generic;
5using BenchmarkDotNet.Attributes;
6using osu.Framework.Extensions.ListExtensions;
7
8namespace osu.Framework.Benchmarks
9{
10 [MemoryDiagnoser]
11 public class BenchmarkSlimReadOnlyCollection
12 {
13 private readonly List<int> list = new List<int> { 0, 1, 2, 3, 4, 5, 3, 2, 3, 1, 4, 5, -1 };
14
15 [Benchmark(Baseline = true)]
16 public int List()
17 {
18 int sum = 0;
19
20 for (int i = 0; i < 1000; i++)
21 {
22 foreach (var v in list)
23 sum += v;
24 }
25
26 return sum;
27 }
28
29 [Benchmark]
30 public int ListAsReadOnly()
31 {
32 int sum = 0;
33
34 for (int i = 0; i < 1000; i++)
35 {
36 foreach (var v in list.AsReadOnly())
37 sum += v;
38 }
39
40 return sum;
41 }
42
43 [Benchmark]
44 public int ListAsSlimReadOnly()
45 {
46 int sum = 0;
47
48 for (int i = 0; i < 1000; i++)
49 {
50 foreach (var v in list.AsSlimReadOnly())
51 sum += v;
52 }
53
54 return sum;
55 }
56 }
57}