Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(NSArray *)letters
- {
- NSMutableArray *letters = [[NSMutableArray alloc] init];
- for (char myChar = 'a'; myChar <= 'z'; myChar++)
- {
- [letters addObject:[NSString stringWithFormat:@"%c", myChar]];
- }
- for (char myChar = 'A'; myChar <= 'Z'; myChar++)
- {
- [letters addObject:[NSString stringWithFormat:@"%c", myChar]];
- }
- for (NSUInteger i = 0; i < 10; i ++)
- {
- [letters addObject:[NSString stringWithFormat:@"%@", @(i)]];
- }
- [letters addObject:@"#"];
- [letters addObject:@" "];
- return letters;
- }
- -(void)createSource
- {
- _dataSource = [[NSMutableArray alloc] init];
- NSArray *letters = [self letters];
- for (NSUInteger i = 0; i < 50000; i ++)
- {
- RealmContact *aContact = [[RealmContact alloc] init];
- NSMutableString *aContactName = [[NSMutableString alloc] init];
- for (NSUInteger i = 0; i < 10; i ++)
- {
- NSUInteger rand = arc4random_uniform((UInt32)[letters count]);
- [aContactName appendString:letters[rand]];
- }
- [aContact setFirstName:aContactName];
- [_dataSource addObject:aContact];
- }
- NSLog(@"------- Method Author -----");
- NSArray *sortedLetters = nil;
- NSMutableDictionary *nameDic = [NSMutableDictionary new];
- NSDate *date = [NSDate date];
- for (RealmContact *contact in _dataSource)
- {
- if (contact.firstName.length>0)
- {
- NSString *firName= [contact.firstName stringByReplacingOccurrencesOfString:@" " withString:@""];
- NSString *regex = @"^[A-Za-z]+";
- NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
- BOOL result = [test evaluateWithObject:firName];
- if (firName.length>0 && result)
- {
- [nameDic setObject:@"firstletter" forKey:[[firName substringToIndex:1]uppercaseString]];
- }else{
- [nameDic setObject:@"firstletter" forKey:@"#"];
- }
- }
- }
- sortedLetters = [[nameDic allKeys]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
- NSLog(@"Time1: %@", @([[NSDate date] timeIntervalSinceDate:date]));
- NSLog(@"------- Alternative Method -----");
- NSArray *sortedLetters2 = nil;
- NSMutableSet *set = [[NSMutableSet alloc] init];
- NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"AZERTYUIOPQSDFGHJKLMWXCVBN"];
- NSDate *date2 = [NSDate date];
- for (RealmContact *contact in _dataSource)
- {
- NSString *firName = [[contact firstName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- if ([firName length])
- {
- unichar c = [[firName uppercaseString] characterAtIndex:0];
- if ([charSet characterIsMember:c])
- {
- [set addObject:[NSString stringWithFormat: @"%C", c]];
- }
- else
- {
- [set addObject:@"#"];
- }
- }
- }
- sortedLetters2 = [[set allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
- NSLog(@"Time2: %@", @([[NSDate date] timeIntervalSinceDate:date2]));
- NSLog(@"sortedLetters: %@", sortedLetters);
- NSLog(@"sortedLetters2: %@", sortedLetters2);
- NSLog(@"sortedLetters %@ sortedLetters2", [sortedLetters isEqualToArray:sortedLetters2]?@"==":@"!=");
- }
- /*
- ------- Method Author -----
- Time1: 0.6597279906272888
- ------- Alternative Method -----
- Time2: 0.05883300304412842
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement