Skip to content

Instantly share code, notes, and snippets.

@randomsequence
Last active June 15, 2018 16:06
Show Gist options
  • Save randomsequence/2140eadd0b96fc53e149e368e0040b44 to your computer and use it in GitHub Desktop.
Save randomsequence/2140eadd0b96fc53e149e368e0040b44 to your computer and use it in GitHub Desktop.
#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;
}
@randomsequence
Copy link
Author

randomsequence commented Jun 15, 2018

Serial:
base    : 0.01133299
rwlock  :+0.00428009 (37.7667%)
dispatch:+0.06589592 (581.4522%)
@sync   :+0.09532309 (841.1116%)
Parallel:
base    : 0.55236197
rwlock  :+0.01732695 (3.1369%)
dispatch:+3.11447012 (563.8459%)
@sync   :+4.23668313 (767.0121%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment