Last active
June 15, 2018 16:06
-
-
Save randomsequence/2140eadd0b96fc53e149e368e0040b44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#include <pthread.h> | |
@interface Getty : NSObject { | |
@protected | |
id _object; | |
} | |
@property (nonatomic, strong, readonly) id object; | |
@end | |
@implementation Getty | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
_object = [NSObject new]; | |
} | |
return self; | |
} | |
- (id)object { | |
return _object; | |
} | |
@end | |
@interface SynchronisedGetty : Getty | |
@end | |
@implementation SynchronisedGetty { | |
} | |
- (id)object { | |
id object = nil; | |
@synchronized (self) { | |
object = _object; | |
} | |
return object; | |
} | |
@end | |
@interface RWLockGetty : Getty | |
@end | |
@implementation RWLockGetty { | |
pthread_rwlock_t _theLock; | |
} | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
pthread_rwlock_init(&_theLock, NULL); | |
} | |
return self; | |
} | |
- (void)dealloc { | |
pthread_rwlock_destroy(&_theLock); | |
} | |
- (id)object { | |
pthread_rwlock_rdlock(&_theLock); | |
return _object; | |
pthread_rwlock_unlock(&_theLock); | |
} | |
@end | |
@interface DispatchGetty : Getty | |
@end | |
@implementation DispatchGetty { | |
dispatch_queue_t _queue; | |
} | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
_queue = dispatch_queue_create("array q", DISPATCH_QUEUE_SERIAL); | |
} | |
return self; | |
} | |
- (void)dealloc { | |
} | |
- (id)object { | |
__block id object = nil; | |
dispatch_sync(_queue, ^(void) { | |
object = _object; | |
}); | |
return object; | |
} | |
@end | |
CFAbsoluteTime timeRun(Getty *getty) { | |
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); | |
for (NSUInteger i=0; i<1000000; i++) { | |
(void)[getty object]; | |
} | |
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent(); | |
return endTime-startTime; | |
} | |
CFAbsoluteTime timeParallelRun(Getty *getty) { | |
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); | |
dispatch_queue_t queue = dispatch_queue_create("parallel queue", DISPATCH_QUEUE_CONCURRENT); | |
dispatch_group_t group = dispatch_group_create(); | |
for (NSUInteger i=0; i<1000000; i++) { | |
dispatch_group_async(group, queue, ^{ | |
(void)[getty object]; | |
}); | |
} | |
dispatch_group_wait(group, DISPATCH_TIME_FOREVER); | |
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent(); | |
return endTime-startTime; | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
CFAbsoluteTime baseTime = timeRun([Getty new]); | |
CFAbsoluteTime rwlockTime = timeRun([RWLockGetty new]) - baseTime; | |
CFAbsoluteTime dispatchTime = timeRun([DispatchGetty new]) - baseTime; | |
CFAbsoluteTime syncTime = timeRun([SynchronisedGetty new]) - baseTime; | |
NSLog(@"Serial:"); | |
NSLog(@"base : %0.8f", baseTime); | |
NSLog(@"rwlock :+%0.8f (%0.4f%%)", rwlockTime, rwlockTime/baseTime*100.0); | |
NSLog(@"dispatch:+%0.8f (%0.4f%%)", dispatchTime, dispatchTime/baseTime*100.0); | |
NSLog(@"@sync :+%0.8f (%0.4f%%)", syncTime, syncTime/baseTime*100.0); | |
NSLog(@"Parallel:"); | |
CFAbsoluteTime baseTimeP = timeParallelRun([Getty new]); | |
CFAbsoluteTime rwlockTimeP = timeParallelRun([RWLockGetty new]) - baseTimeP; | |
CFAbsoluteTime dispatchTimeP = timeParallelRun([DispatchGetty new]) - baseTimeP; | |
CFAbsoluteTime syncTimeP = timeParallelRun([SynchronisedGetty new]) - baseTimeP; | |
NSLog(@"base : %0.8f", baseTimeP); | |
NSLog(@"rwlock :+%0.8f (%0.4f%%)", rwlockTimeP, rwlockTimeP/baseTimeP*100.0); | |
NSLog(@"dispatch:+%0.8f (%0.4f%%)", dispatchTimeP, dispatchTimeP/baseTimeP*100.0); | |
NSLog(@"@sync :+%0.8f (%0.4f%%)", syncTimeP, syncTimeP/baseTimeP*100.0); | |
} | |
return 0; | |
} |
Author
randomsequence
commented
Jun 15, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment