Reputation: 3165
Question is the same as the title. ("sortedArrayHint" method of NSArray class, what is the purpose of this method and how is it used)
I read documentation but the explanation is not clear.
Please explain the purpose of this method and its usage.
Upvotes: 5
Views: 1159
Reputation: 36752
The idea is simple. Assume you have a large array that should always be sorted. Changing or inserting even one element means you have to resort the array. Sorting is costly.
The method -[NSArray sortedArrayHint]
can be called on an already sorted array in order to get private internal data that can be used to speed up a sort of the same array given that only a small change has been made.
Usage is simple:
-[NSArray sortedArrayHint]
.-[NSArray sortedArrayUsingFunction:context:hint:]
with the stored hint.-[NSArray sortedArrayUsingFunction:context:]
, and get a new hint.What is a small, or large, change is something you must measure with Instruments.
I never use this myself, since I have found it more effective to use my own categories on NSArray
and NSMutabelArray
for sorted inserts, that uses a binary search, on sorted array. My code is available as open source here: https://github.com/Jayway/CWFoundation
Upvotes: 10