Last active
October 7, 2016 14:18
-
-
Save wongzigii/b707c0834f3a7e2223db to your computer and use it in GitHub Desktop.
Benchmark of Lock Performance Test
This file contains hidden or 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 <objc/runtime.h> | |
#import <objc/message.h> | |
#import <libkern/OSAtomic.h> | |
#import <pthread.h> | |
#define ITERATIONS (1024*1024*32) | |
- (void)testLock | |
{ | |
double then, now; | |
unsigned int i, count; | |
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | |
OSSpinLock spinlock = OS_SPINLOCK_INIT; | |
@autoreleasepool { | |
NSLock *lock = [NSLock new]; | |
then = CFAbsoluteTimeGetCurrent(); | |
for(i=0;i<ITERATIONS;++i) | |
{ | |
[lock lock]; | |
[lock unlock]; | |
} | |
now = CFAbsoluteTimeGetCurrent(); | |
printf("NSLock: %f sec\n", now-then); | |
then = CFAbsoluteTimeGetCurrent(); | |
for(i=0;i<ITERATIONS;++i) | |
{ | |
pthread_mutex_lock(&mutex); | |
pthread_mutex_unlock(&mutex); | |
} | |
now = CFAbsoluteTimeGetCurrent(); | |
printf("pthread_mutex: %f sec\n", now-then); | |
then = CFAbsoluteTimeGetCurrent(); | |
for(i=0;i<ITERATIONS;++i) | |
{ | |
OSSpinLockLock(&spinlock); | |
OSSpinLockUnlock(&spinlock); | |
} | |
now = CFAbsoluteTimeGetCurrent(); | |
printf("OSSpinlock: %f sec\n", now-then); | |
id obj = [NSObject new]; | |
then = CFAbsoluteTimeGetCurrent(); | |
for(i=0;i<ITERATIONS;++i) | |
{ | |
@synchronized(obj) | |
{ | |
} | |
} | |
now = CFAbsoluteTimeGetCurrent(); | |
printf("@synchronized: %f sec\n", now-then); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment