From 98e787512fef0577a28ded39846f3900cf2c94e1 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Thu, 31 Oct 2024 13:42:31 +0900 Subject: [PATCH] Add/improve benchmarks --- .../BenchmarkBindableList.cs | 77 ++++++++++++++----- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/osu.Framework.Benchmarks/BenchmarkBindableList.cs b/osu.Framework.Benchmarks/BenchmarkBindableList.cs index 9cb5cb6f6..945ae98b2 100644 --- a/osu.Framework.Benchmarks/BenchmarkBindableList.cs +++ b/osu.Framework.Benchmarks/BenchmarkBindableList.cs @@ -1,22 +1,69 @@ // Copyright (c) ppy Pty Ltd . 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 list = new BindableList(); - private IBindableList 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[] lists = null!; [GlobalSetup] public void GlobalSetup() { - for (int i = 0; i < 10; i++) - list.Add(i); + lists = new BindableList[NumBindings + 1]; + + lists[0] = new BindableList(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 void Add() + { + setupList().Add(1); + } + + [Benchmark] + public void Remove() + { + setupList().Remove(0); + } + + [Benchmark] + public void Clear() + { + setupList().Clear(); + } + + [Benchmark] + public void AddRange() + { + setupList().AddRange(small_data); + } + + [Benchmark] + public void SetIndex() + { + setupList()[0]++; } [Benchmark] @@ -24,27 +71,17 @@ namespace osu.Framework.Benchmarks { int result = 0; - for (int i = 0; i < 100; i++) - { - foreach (int val in list) - result += val; - } + foreach (int val in setupList()) + result += val; return result; } - [Benchmark] - public int EnumerateInterface() + private BindableList setupList() { - int result = 0; - - for (int i = 0; i < 100; i++) - { - foreach (int val in iList) - result += val; - } - - return result; + lists[0].Clear(); + lists[0].Add(0); + return lists[0]; } } }