Skip to content

Commit

Permalink
Add/improve benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Dec 10, 2024
1 parent f3af3c9 commit 98e7875
Showing 1 changed file with 55 additions and 18 deletions.
73 changes: 55 additions & 18 deletions osu.Framework.Benchmarks/BenchmarkBindableList.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,87 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Linq;
using BenchmarkDotNet.Attributes;
using osu.Framework.Bindables;

namespace osu.Framework.Benchmarks
{
[MemoryDiagnoser]
[WarmupCount(0)]
[IterationCount(2)]
public class BenchmarkBindableList
{
private readonly BindableList<int> list = new BindableList<int>();
private IBindableList<int> iList => list;
private static readonly int[] small_data = Enumerable.Range(0, 10).ToArray();

[Params(0, 1, 10, 20)]
public int NumBindings { get; set; }

private BindableList<int>[] lists = null!;

[GlobalSetup]
public void GlobalSetup()
{
for (int i = 0; i < 10; i++)
list.Add(i);
lists = new BindableList<int>[NumBindings + 1];

lists[0] = new BindableList<int>(Enumerable.Range(0, 10000).ToArray());
for (int i = 1; i < lists.Length; i++)
lists[i] = lists[i - 1].GetBoundCopy();
lists[0].Clear();
}

[Benchmark(Baseline = true)]
public void Create()
{
setupList();
}

[Benchmark]
public int Enumerate()
public void Add()
{
int result = 0;
setupList().Add(1);
}

for (int i = 0; i < 100; i++)
{
foreach (int val in list)
result += val;
}
[Benchmark]
public void Remove()
{
setupList().Remove(0);
}

return result;
[Benchmark]
public void Clear()
{
setupList().Clear();
}

[Benchmark]
public int EnumerateInterface()
public void AddRange()
{
setupList().AddRange(small_data);
}

[Benchmark]
public void SetIndex()
{
setupList()[0]++;
}

[Benchmark]
public int Enumerate()
{
int result = 0;

for (int i = 0; i < 100; i++)
{
foreach (int val in iList)
result += val;
}
foreach (int val in setupList())
result += val;

return result;
}

private BindableList<int> setupList()
{
lists[0].Clear();
lists[0].Add(0);
return lists[0];
}
}
}

0 comments on commit 98e7875

Please sign in to comment.