Hi,
you can check the performance with this code:
Imports System.ComponentModel
Namespace WpfApp072
Public Class ViewModel
Implements INotifyPropertyChanged
Public Sub New()
start()
End Sub
Public Property HAlignment1 As String = "Left"
Public Property HAlignment2 As HorizontalAlignment = HorizontalAlignment.Left
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Async Sub start()
Await Task.Delay(1000)
Dim sw As New Stopwatch
sw.Start()
For i = 1 To 10000
If HAlignment1 = "Left" Then
HAlignment1 = "Center"
ElseIf HAlignment1 = "Center" Then
HAlignment1 = "Right"
ElseIf HAlignment1 = "Right" Then
HAlignment1 = "Left"
End If
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(HAlignment1)))
Next
Debug.WriteLine($"String: {sw.ElapsedMilliseconds} ms")
sw.Restart()
For i = 1 To 10000
If HAlignment2 = HorizontalAlignment.Left Then
HAlignment2 = HorizontalAlignment.Center
ElseIf HAlignment2 = HorizontalAlignment.Center Then
HAlignment2 = HorizontalAlignment.Right
ElseIf HAlignment2 = HorizontalAlignment.Right Then
HAlignment2 = HorizontalAlignment.Left
End If
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(HAlignment1)))
Next
Debug.WriteLine($"HorizontalAlignment: {sw.ElapsedMilliseconds} ms")
For i = 1 To 10000
If HAlignment1 = "Left" Then
HAlignment1 = "Center"
ElseIf HAlignment1 = "Center" Then
HAlignment1 = "Right"
ElseIf HAlignment1 = "Right" Then
HAlignment1 = "Left"
End If
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(HAlignment1)))
Next
Debug.WriteLine($"String: {sw.ElapsedMilliseconds} ms")
sw.Restart()
For i = 1 To 10000
If HAlignment2 = HorizontalAlignment.Left Then
HAlignment2 = HorizontalAlignment.Center
ElseIf HAlignment2 = HorizontalAlignment.Center Then
HAlignment2 = HorizontalAlignment.Right
ElseIf HAlignment2 = HorizontalAlignment.Right Then
HAlignment2 = HorizontalAlignment.Left
End If
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(HAlignment1)))
Next
Debug.WriteLine($"HorizontalAlignment: {sw.ElapsedMilliseconds} ms")
End Sub
End Class
End Namespace
Result:
String: 42 ms
HorizontalAlignment: 25 ms
String: 67 ms
HorizontalAlignment: 38 ms