Created
November 26, 2021 09:11
-
-
Save grokys/fe7a87dc95d48fe09b4aac40e7201b7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
namespace Avalonia.Benchmarks.Data | |
{ | |
[MemoryDiagnoser, InProcess] | |
public class PropertyAddClassHandlerBenchmarks | |
{ | |
private const int Iterations = 100; | |
[Benchmark(Baseline = true)] | |
public void AddClassHandler() | |
{ | |
var target = new AddClassHandlerTest(); | |
for (var i = 0; i < Iterations; ++i) | |
target.Foo = i; | |
} | |
[Benchmark] | |
public void OnPropertyChanged() | |
{ | |
var target = new OnPropertyChangedTest(); | |
for (var i = 0; i < Iterations; ++i) | |
target.Foo = i; | |
} | |
private class AddClassHandlerTest : AvaloniaObject | |
{ | |
public static StyledProperty<int> FooProperty = | |
AvaloniaProperty.Register<AddClassHandlerTest, int>("Foo"); | |
static AddClassHandlerTest() | |
{ | |
FooProperty.Changed.AddClassHandler<AddClassHandlerTest, int>((x, e) => x.FooChanged(e)); | |
} | |
public int Foo | |
{ | |
get => GetValue(FooProperty); | |
set => SetValue(FooProperty, value); | |
} | |
public int LastValue { get; private set; } | |
private void FooChanged(AvaloniaPropertyChangedEventArgs<int> e) => LastValue = e.NewValue.Value; | |
} | |
private class OnPropertyChangedTest : AvaloniaObject | |
{ | |
public static StyledProperty<int> FooProperty = | |
AvaloniaProperty.Register<AddClassHandlerTest, int>("Foo"); | |
public int Foo | |
{ | |
get => GetValue(FooProperty); | |
set => SetValue(FooProperty, value); | |
} | |
public int LastValue { get; private set; } | |
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change) | |
{ | |
base.OnPropertyChanged(change); | |
if (change.Property == FooProperty) | |
LastValue = change.NewValue.GetValueOrDefault<int>(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment