Advertisement
Don_Mag

Untitled

May 4th, 2021
2,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface SimpleClass : NSObject
  4.  
  5. @property (assign, readwrite) float value;
  6.  
  7. @end
  8.  
  9. @implementation SimpleClass
  10.  
  11. @end
  12.  
  13. @interface ComplexClass : NSObject
  14.  
  15. @property (strong, nonatomic) NSString *firstName;
  16. @property (strong, nonatomic) NSString *lastName;
  17. @property (assign, readwrite) float value;
  18. @property (strong, nonatomic) NSArray *arr;
  19.  
  20. @end
  21.  
  22. @implementation ComplexClass
  23.  
  24. @end
  25.  
  26. @interface TimingViewController : UIViewController
  27.  
  28. @property (strong, nonatomic) NSMutableArray *simpleArray;
  29. @property (strong, nonatomic) NSMutableArray *complexArray;
  30.  
  31. @end
  32.  
  33. @implementation TimingViewController
  34.  
  35. int numElements = 1000;
  36. int numReps = 50;
  37.  
  38. - (void)viewDidLoad {
  39.     [super viewDidLoad];
  40.    
  41.     self.view.backgroundColor = [UIColor systemYellowColor];
  42.    
  43.     _simpleArray = [NSMutableArray new];
  44.     _complexArray = [NSMutableArray new];
  45.  
  46.     srand48(time(0));
  47.  
  48.     for (int i = 0; i < numElements; i++) {
  49.        
  50.         float r = drand48();
  51.        
  52.         SimpleClass *sc = [SimpleClass new];
  53.         sc.value = r;
  54.         [_simpleArray addObject:sc];
  55.        
  56.         ComplexClass *cc = [ComplexClass new];
  57.         cc.value = r;
  58.         cc.firstName = [NSString stringWithFormat:@"First %i", i];
  59.         cc.lastName = [NSString stringWithFormat:@"Last %i", i];
  60.        
  61.         NSMutableArray *a = [NSMutableArray new];
  62.         for (int j = 0; j < 1000; j++) {
  63.             [a addObject:[NSNumber numberWithInt:j]];
  64.         }
  65.         cc.arr = [a mutableCopy];
  66.        
  67.         [_complexArray addObject:cc];
  68.     }
  69.    
  70. }
  71.  
  72. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  73.    
  74.     double d = 0;
  75.    
  76.     NSLog(@"Sorting %d element array of Simple Objects %d times...", numElements, numReps);
  77.    
  78.     NSMutableArray *a = [NSMutableArray new];
  79.     for (int j = 0; j < numReps; j++) {
  80.         d = [self timingTest:_simpleArray];
  81.         [a addObject:[NSNumber numberWithDouble:d]];
  82.     }
  83.  
  84.     NSLog(@"Sorting %d element array of Complex Objects %d times...", numElements, numReps);
  85.  
  86.     NSMutableArray *b = [NSMutableArray new];
  87.     for (int j = 0; j < numReps; j++) {
  88.         d = [self timingTest:_complexArray];
  89.         [b addObject:[NSNumber numberWithDouble:d]];
  90.     }
  91.  
  92.     NSNumber *aAvg = [a valueForKeyPath:@"@avg.self"];
  93.     NSNumber *bAvg = [b valueForKeyPath:@"@avg.self"];
  94.  
  95.     NSLog(@"Average Seconds for Simple:  %0.10f", [aAvg doubleValue]);
  96.     NSLog(@"Average Seconds for Complex: %0.10f", [bAvg doubleValue]);
  97.  
  98. }
  99.  
  100. - (double)timingTest: (NSMutableArray *)a {
  101.    
  102.     CFTimeInterval startTime;
  103.     CFTimeInterval endTime;
  104.  
  105.     NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES];
  106.  
  107.     startTime = CACurrentMediaTime();
  108.     NSArray *aa = [a sortedArrayUsingDescriptors:@[sortDescriptor]];
  109.     endTime = CACurrentMediaTime();
  110.    
  111.     return endTime - startTime;
  112. }
  113.  
  114. @end
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement