Re: Better sorting using threads?
Re: Better sorting using threads?
- Subject: Re: Better sorting using threads?
- From: Ken Thomases <email@hidden>
- Date: Thu, 11 Mar 2010 00:34:10 -0600
On Mar 10, 2010, at 11:50 PM, Graham Cox wrote:
> I've got a situation where sorting an array using sort descriptors is slow. The number of items being sorted is not enormous - about 2,000 - but there could be several sort descriptors and the main string one is using a localized and natural numeric comparison. I cache the results and take a lot of care only to resort when needed, but that doesn't help on the first run through which often takes in the order of 10-20 seconds. The interface that displays the results therefore beachballs for this time.
>
> I'm considering using threads to help, where a thread is used to perform the sort itself, and until the sort has finished the UI can display a busy indicator (indeterminate circular progress indicator) next to the selected row in the table. This is in the master table of a master-detail interface where the detail displays the sorted items selected. While busy the detail view can be blank or show the previous arrangement.
>
> So my question is, is threading a good solution? While NSMutableArray isn't marked as thread-safe, the array in question can be arranged to not be used outside of the sorting thread, and only substituted for the returned sorted items when complete. Or can array sorting not be done on a thread at all?
This is a sensible solution. NSMutableArray is safe so long as only one thread is accessing it at a time.
However, sorting 2000 items should not take 10-20 seconds! Have you profiled to find out what's actually taking the time?
If a property of the objects has to be massaged into another form before the comparison can take place, it can be a performance win to do that massaging once for each object before the sort and cache the result. Otherwise, it may end up being performed over and over, each time one object is compared to another. (I wouldn't worry about the localized, numeric comparison of strings, though. I don't think there's a good way to preflight those, and I wouldn't think that would be a significant factor in the slowness. But, of course, don't trust my intuition -- measure.)
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden