19

I need to watch properties for changes. Which method is better in terms of performance and memory use: implementing INotifyPropertyChanged or using a DependencyProperty?

Note: Yes, I have read the other question INotifyPropertyChanged vs. DependencyProperty in ViewModel.

Community
  • 1
  • 1
  • 3
    Why don't you explain why the other article failed to give you your answer, or what makes your question special? – Robert P Feb 20 '09 at 18:16
  • While I found the answer myself in another question (see my answer), I also found Rob McCready's answer very helpful which is the reason I selected his answer instead and gave him a thumbs up. Thanks everyone! –  Feb 20 '09 at 18:26

6 Answers6

21

Memory Use: INotifyPropertyChanged is an interface, so close to zero memory overhead. "Close to zero" because I assume you'll be writing an OnPropertyChanged method and maybe some event handlers in other classes (unless you're really just talking about binding to WPF), so there will be a slight code overhead.

Performance: DependancyProperties have alot going on under the covers. Unless you write the most non-performant OnPropertyChanged method ever, I would wager that INotifyPropertyChanged will be the perf winner as well.

Unless you have a defined reason for wanting/needing the behaviors provided by a DP I would just go with INotifyPropertyChanged.

Update

As the comment mentions binding performance is a bit faster for DPs (15-20% faster, but still only a difference of less than 50ms for 1000 binds) due to the amount of reflection needed to to the lookup/hookup of direct properties. This is technically different than the performance of updating a databound UI element which is what my comment was geared towards. But that doesn't mean my wager is still correct either. So a few examples and alot of .NET Reflector digger later it looks... inconclusive. Both paths do a ton of work under the covers and I wasn't able to get any examples to show a definitive difference in update performance.

I still stick with INotifyPropertyChanged unless I have specific need for DPs, but it was at least an interesting exercise to poke around into the WPF core some more. :)

Rob McCready
  • 1,909
  • 1
  • 19
  • 20
13

Never mind, I just found the answers I was looking for in the following question.

I'll repost the answer by "LBugnion" (so all credit goes to him) for your convenience:


Implementing INotifyPropertyChanged has many advantages over DependencyObjects (I will abbreviate this DO to make it easier) and using DependencyProperties (DPs):

  • This is more lightweight
  • Allows you more freedom in modeling your objects
  • Can be serialized easily
  • You can raise the event when you want, which can be useful in certain scenarios, for example when you want to bundle multiple changes in only one UI operation, or when you need to raise the event even if the data didn't change (to force redraw...)

On the other hand, inheriting DOs in WPF have the following advantages:

  • Easier to implement especially for beginners.
  • You get a callback mechanism (almost) for free, allowing you to be notified when the property value changes
  • You get a coercion mechanism with allows you to define rules for max, min and present value of the property.

There are other considerations, but these are the main.

I think the general consensus is that DPs are great for controls (and you can implement a CustomControl with custom DPs even in Silverlight), but for data objects you should rather implement INotifyPropertyChanged.

HTH, Laurent

Community
  • 1
  • 1
11

As MSDN says, WPF is quicker at binding to DependencyProperties than to custom CLR objects using INotifyPropertyChanged.

See http://msdn.microsoft.com/en-us/library/bb613546.aspx

Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
1

It is very interesting and important question IMHO. I had tested the it on next pure (without binding) task. DP as a fact must be very fast... (100.. properties of each standard control, animatable) So, the problem is

Assume the 3D Coordinate Axes. We put a labels like x, y and z near each axis. 3D Text rendering in WPF is relatively hard work WPF 3D Perfomance

The task is rotation detected labels at mouse rotation within 3D scene by the way when labels have a direct view to user (At 3D scene rotation they are translating with axes but rotating by special way). This task is solved by the same CallBack function. Implementation looks like this:

  1. Approach - INotifyPropertyChanged includes)

    Public Property T3D_LabelAngleX() As Double Get Return _T3D_LabelAngleX End Get Set(ByVal value As Double) _T3D_LabelAngleX = value t3d_SetLabelTransform() End Set End Property

  2. Approach - DP

    Public Shared ReadOnly MDS_AngleLabelRotation_XProperty As DependencyProperty = _ DependencyProperty.Register("MDS_AxisAngleRotation_X", _ GetType(Double), GetType(MainDecartsSystem), _ New PropertyMetadata(MDS_AngleLabelRotation_XDefault, AddressOf MDS_RotationChanged))

    Public Property MDS_AngleLabelRotation_X() As Double
        Get
            Return CType(GetValue(MDS_AngleLabelRotation_XProperty), Double)
        End Get
        Set(ByVal value As Double)
            SetValue(MDS_AngleLabelRotation_XProperty, value)
        End Set
    End Property
    

    The focus is MDS_RotationChanged and t3d_SetLabelTransform() are equal. Labels in rotation.... My poor mouse.... BOTH are very FAST! I could not to detect any difference. It seems that DP a little bit faster, probably... (I an not sure). Thank for the question.

Sergey Orlov
  • 491
  • 5
  • 16
1

IMHO, INotifyPropertyChanged is very lightweight and easy to use in comparison to DPs. As far as performance/memory is concerned, do you really need to know? Knuth wants to know.

Obviously, you have to determine your requirements and see how many DP's you're expecting to create/second, how long lived they are, and run a few tests. I've done this before and didn't see any issues with creating short-lived DP's at ~10k/sec (YMMV).

1

I would suggest that you are asking the wrong question. Instead of asking about performance you should be looking at the features and benefit of each method. Basically, the decision of which to use should not be based on performance. From the bit of reading I've done if you are creating a control then you should use dependencyProperty, otherwise use INotifyPropertyChanged.

MikeKulls
  • 2,979
  • 2
  • 25
  • 30
  • It is not so simple way: Control - DP, otherwise - INotifyPropertyChanged. For example, we inherits from ModelVisual3D (so I can implement a DP) within 3D Graphics class. I am in interest to fast work of my class instances on 3D scene. Also, I can inherit from FrameworkElement any my class for implementing DP. The one thing I am in interest in all cases - performance! – Sergey Orlov Nov 16 '12 at 19:32