Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new ObservableCollection vs adding items in a loop

In terms of speed and the amount of notifications generated, is this code:

ObservableCollection<Foo> foo = new ObservableCollection<Foo>(bar);
this.SomeProperty = foo;

the same as:

this.SomeProperty = new ObservableCollection<Foo>();

foreach (var v in bar) 
{
    this.SomeProperty.Add(v);
}

If they are the same, is it possible to somehow turn off the notifications generated?

Objective: I'm trying to speed up the display of Telerik RadChart in silverlight. It seems to take a while to display (and freezes the in browser app) even after the property containing ObservableCollection is set. Once the chart is drawn everything works correctly.

like image 651
ghostJago Avatar asked Feb 01 '12 15:02

ghostJago


1 Answers

  1. Profile it or Test it! According to the docs, the PropertyChanged event occcurs when an item is added, removed, changed, moved, or the entire list is refreshed. You can therefore write some test code that just subscribes to this event and see what happens.

  2. Fast performing and thread safe observable collection - if it is due to a constant barrage of OnChanged events, consider only firing after a bulk update – someone has done this work for you already

  3. http://www.telerik.com/help/silverlight/radchart-performance-tips-and-tricks.html deals specifically with the scenario you describe about half way down the page. Their conclusion is the same as 2 - in fact, the code looks very similar :-)

If the freeze is occuring before the binding actually takes place, then I'd make sure that the delay isn't actually rendering based or because of another actvity (like the time taken to load the collection). Again, profiling is your friend.

like image 118
dash Avatar answered Nov 15 '22 04:11

dash