Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(NSArray *)letters
  2. {
  3.     NSMutableArray *letters = [[NSMutableArray alloc] init];
  4.     for (char myChar = 'a'; myChar <= 'z'; myChar++)
  5.     {
  6.         [letters addObject:[NSString stringWithFormat:@"%c", myChar]];
  7.     }
  8.     for (char myChar = 'A'; myChar <= 'Z'; myChar++)
  9.     {
  10.         [letters addObject:[NSString stringWithFormat:@"%c", myChar]];
  11.     }
  12.     for (NSUInteger i = 0; i < 10; i ++)
  13.     {
  14.         [letters addObject:[NSString stringWithFormat:@"%@", @(i)]];
  15.     }
  16.    
  17.     [letters addObject:@"#"];
  18.     [letters addObject:@" "];
  19.    
  20.     return letters;
  21. }
  22. -(void)createSource
  23. {
  24.     _dataSource = [[NSMutableArray alloc] init];
  25.    
  26.     NSArray *letters = [self letters];
  27.    
  28.     for (NSUInteger i = 0; i < 50000; i ++)
  29.     {
  30.         RealmContact *aContact = [[RealmContact alloc] init];
  31.         NSMutableString *aContactName = [[NSMutableString alloc] init];
  32.         for (NSUInteger i = 0; i < 10; i ++)
  33.         {
  34.             NSUInteger rand = arc4random_uniform((UInt32)[letters count]);
  35.             [aContactName appendString:letters[rand]];
  36.         }
  37.         [aContact setFirstName:aContactName];
  38.         [_dataSource addObject:aContact];
  39.     }
  40.    
  41.     NSLog(@"------- Method Author -----");
  42.     NSArray *sortedLetters = nil;
  43.     NSMutableDictionary *nameDic = [NSMutableDictionary new];
  44.     NSDate *date = [NSDate date];
  45.     for (RealmContact *contact in _dataSource)
  46.     {
  47.         if (contact.firstName.length>0)
  48.         {
  49.             NSString *firName= [contact.firstName stringByReplacingOccurrencesOfString:@" " withString:@""];
  50.             NSString *regex = @"^[A-Za-z]+";
  51.             NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  52.             BOOL result = [test evaluateWithObject:firName];
  53.            
  54.             if (firName.length>0 && result)
  55.             {
  56.                 [nameDic setObject:@"firstletter" forKey:[[firName substringToIndex:1]uppercaseString]];
  57.             }else{
  58.                 [nameDic setObject:@"firstletter" forKey:@"#"];
  59.             }
  60.         }
  61.     }
  62.     sortedLetters = [[nameDic allKeys]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
  63.     NSLog(@"Time1: %@", @([[NSDate date] timeIntervalSinceDate:date]));
  64.    
  65.     NSLog(@"------- Alternative Method -----");
  66.     NSArray *sortedLetters2 = nil;
  67.     NSMutableSet *set = [[NSMutableSet alloc] init];
  68.     NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"AZERTYUIOPQSDFGHJKLMWXCVBN"];
  69.     NSDate *date2 = [NSDate date];
  70.     for (RealmContact *contact in _dataSource)
  71.     {
  72.         NSString *firName = [[contact firstName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  73.         if ([firName length])
  74.         {
  75.             unichar c = [[firName uppercaseString] characterAtIndex:0];
  76.             if ([charSet characterIsMember:c])
  77.             {
  78.                 [set addObject:[NSString stringWithFormat: @"%C", c]];
  79.             }
  80.             else
  81.             {
  82.                 [set addObject:@"#"];
  83.             }
  84.         }
  85.     }
  86.     sortedLetters2 = [[set allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
  87.     NSLog(@"Time2: %@", @([[NSDate date] timeIntervalSinceDate:date2]));
  88.    
  89.     NSLog(@"sortedLetters: %@", sortedLetters);
  90.     NSLog(@"sortedLetters2: %@", sortedLetters2);
  91.     NSLog(@"sortedLetters %@ sortedLetters2", [sortedLetters isEqualToArray:sortedLetters2]?@"==":@"!=");
  92. }
  93.  
  94. /*
  95. ------- Method Author -----
  96. Time1: 0.6597279906272888
  97. ------- Alternative Method -----
  98. Time2: 0.05883300304412842
  99. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement