Don_Mag icon

Untitled

Don_Mag | PRO | 05/04/21 04:03:45 PM UTC | 0 ⭐ | 10030 👁️ | Never ⏰ | []
Objective C |

0 B

|

None

|

0 👍

/

0 👎

#import <UIKit/UIKit.h>
 
@interface SimpleClass : NSObject
 
@property (assign, readwrite) float value;
 
@end
 
@implementation SimpleClass
 
@end
 
@interface ComplexClass : NSObject
 
@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (assign, readwrite) float value;
@property (strong, nonatomic) NSArray *arr;
 
@end
 
@implementation ComplexClass
 
@end
 
@interface TimingViewController : UIViewController
 
@property (strong, nonatomic) NSMutableArray *simpleArray;
@property (strong, nonatomic) NSMutableArray *complexArray;
 
@end
 
@implementation TimingViewController
 
int numElements = 1000;
int numReps = 50;
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor systemYellowColor];
    
    _simpleArray = [NSMutableArray new];
    _complexArray = [NSMutableArray new];
 
    srand48(time(0));
 
    for (int i = 0; i < numElements; i++) {
        
        float r = drand48();
        
        SimpleClass *sc = [SimpleClass new];
        sc.value = r;
        [_simpleArray addObject:sc];
        
        ComplexClass *cc = [ComplexClass new];
        cc.value = r;
        cc.firstName = [NSString stringWithFormat:@"First %i", i];
        cc.lastName = [NSString stringWithFormat:@"Last %i", i];
        
        NSMutableArray *a = [NSMutableArray new];
        for (int j = 0; j < 1000; j++) {
            [a addObject:[NSNumber numberWithInt:j]];
        }
        cc.arr = [a mutableCopy];
        
        [_complexArray addObject:cc];
    }
    
}
 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    double d = 0;
    
    NSLog(@"Sorting %d element array of Simple Objects %d times...", numElements, numReps);
    
    NSMutableArray *a = [NSMutableArray new];
    for (int j = 0; j < numReps; j++) {
        d = [self timingTest:_simpleArray];
        [a addObject:[NSNumber numberWithDouble:d]];
    }
 
    NSLog(@"Sorting %d element array of Complex Objects %d times...", numElements, numReps);
 
    NSMutableArray *b = [NSMutableArray new];
    for (int j = 0; j < numReps; j++) {
        d = [self timingTest:_complexArray];
        [b addObject:[NSNumber numberWithDouble:d]];
    }
 
    NSNumber *aAvg = [a valueForKeyPath:@"@avg.self"];
    NSNumber *bAvg = [b valueForKeyPath:@"@avg.self"];
 
    NSLog(@"Average Seconds for Simple:  %0.10f", [aAvg doubleValue]);
    NSLog(@"Average Seconds for Complex: %0.10f", [bAvg doubleValue]);
 
}
 
- (double)timingTest: (NSMutableArray *)a {
    
    CFTimeInterval startTime;
    CFTimeInterval endTime;
 
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES];
 
    startTime = CACurrentMediaTime();
    NSArray *aa = [a sortedArrayUsingDescriptors:@[sortDescriptor]];
    endTime = CACurrentMediaTime();
    
    return endTime - startTime;
}
 
@end
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎