Files
osu-framework/osu.Framework.Benchmarks/BenchmarkBindableInstantiation.cs
2022-11-22 13:50:49 +09:00

37 lines
1.1 KiB
C#

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using BenchmarkDotNet.Attributes;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
namespace osu.Framework.Benchmarks
{
[MemoryDiagnoser]
public class BenchmarkBindableInstantiation
{
[Benchmark]
public Bindable<int> Instantiate() => new Bindable<int>();
[Benchmark]
public Bindable<int> GetBoundCopy() => new Bindable<int>().GetBoundCopy();
[Benchmark(Baseline = true)]
public Bindable<int> GetBoundCopyOld() => new BindableOld<int>().GetBoundCopy();
[Benchmark]
public Bindable<int> GetUnboundCopy() => new Bindable<int>().GetUnboundCopy();
private class BindableOld<T> : Bindable<T> where T : notnull
{
public BindableOld(T defaultValue = default!)
: base(defaultValue)
{
}
protected override Bindable<T> CreateInstance() => (BindableOld<T>)Activator.CreateInstance(GetType(), Value).AsNonNull();
}
}
}