问题描述
我需要看的属性更改。哪一种方法是在性能和内存使用方面更好:实施 INotifyPropertyChanged的
或使用的DependencyProperty
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.
推荐答案
内存使用:INotifyPropertyChanged的是一个接口,如此接近为零内存开销。 接近零,因为我相信你会写一个OnPropertyChanged方法,也许在其他类中的某些事件处理(除非你真的只是在谈论绑定到WPF),所以会有轻微的代码开销。
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.
性能:在幕后DependancyProperties有很多的事情。除非你写的大多数非高性能OnPropertyChanged方法过,我敢打赌,INotifyPropertyChanged的将是PERF胜者也。
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.
除非你有想要一个定义的原因/需要通过DP我只想与INotifyPropertyChanged的去提供的行为。
Unless you have a defined reason for wanting/needing the behaviors provided by a DP I would just go with INotifyPropertyChanged.
更新
就像注解中提到约束力的性能是DPS的有点快(15-20%的速度,但仍然只有1000绑定的小于50ms的区别),由于需要直接属性的查找/联播的反射量。这是比更新这就是我的意见正朝着面向数据绑定UI元素的性能技术上是不同的。但是,这并不意味着我的赌注仍然是正确的无论是。因此,.net反射挖掘机的几个例子,并后来很多看起来...没有定论。两个路径做大量的工作在幕后,我没能得到任何例子,说明在更新性能一个明确的区别。
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.
我仍然坚持INotifyPropertyChanged的,除非我有DPS的特殊需要,但它至少是一个有趣的练习闲逛到WPF核心的更多一些。 :)
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. :)
这篇关于INotifyPropertyChanged的主场迎战的DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!