0

I need to search in a big NSMutableArray of NSDictionary with about 50.000 record. I try with:

for (NSDictionary *aProduct in self.arrProduct) {
    if ([[aProduct objectForKey:@"Name"]rangeOfString:@"search string" options:NSCaseInsensitiveSearch].location != NSNotFound) {
        //got it
    }
}

but with 50.000 object, this function had performance very bad. I do not want to use Sqlite to search. Any idea?

tks,

thienlode
  • 68
  • 1
  • 8

3 Answers3

0

You can try using enumerateObjectsUsingBlock: instead of fast enumeration.

Take a look at this for more details.

Also: enumurateObjectsUsingBlock Vs for loops

Community
  • 1
  • 1
Lneuner
  • 1,090
  • 1
  • 9
  • 19
  • It will still be slow. – trojanfoe Nov 09 '15 at 09:54
  • 50k records is a lot, but enumerateObjectsUsingBlocks with still be faster or as fast. To quote the 2nd link: "enumerateObjectsUsingBlock: will be as fast or faster than fast enumeration (for(... in ...) uses the NSFastEnumeration support to implement enumeration). Fast enumeration requires translation from an internal representation to the representation for fast enumeration. There is overhead therein. " – Lneuner Nov 09 '15 at 09:54
  • I'm not sure how much faster it will be in your case, but you can compare them and let us know the results – Lneuner Nov 09 '15 at 09:57
  • It isn't the enumeration that will be slow. It will be the `rangeOfString`. Even with sql this is an expensive search. – Paulw11 Nov 09 '15 at 09:58
  • The array needs to be thrown away in favour of a binary tree or somesuch. – trojanfoe Nov 09 '15 at 09:58
  • i changed to Sqlite because my custom have an other store with 200k product :( – thienlode Nov 09 '15 at 10:52
0

If each data has an unique key, and what you want is searching with an unique key, then you can use Hash table.

Or, the search key is a number, and you need range search, ex key > 5 && key < 10. Then I think ordered binary tree will be helpful. The implementation of binary tree might be found in the GitHub, I supports to someone had done it already.


As for me, I prefer to use SQLite to solve this kind problem. Adding an index to the search key will faster the search performance. I don't like to store huge data in the main memory.

AechoLiu
  • 17,522
  • 9
  • 100
  • 118
  • yes, i changed to SQLite because i found an other store with 200k products. and can not use NSUserDefault to cache. – thienlode Nov 09 '15 at 10:51
  • If you need cache, maybe [TMCache](https://github.com/tumblr/TMCache) can help you. Generally, `Database system` will implement some cache mechanism to increase performance. I use `SQLite cache` as keywords and google gives many posts. – AechoLiu Nov 09 '15 at 12:09
0

finally, i use CoreData. Array can not run smooth with big size data.

thienlode
  • 68
  • 1
  • 8