Skip to content

Instantly share code, notes, and snippets.

@Coneboy-k
Created August 21, 2014 09:13
Show Gist options
  • Save Coneboy-k/898ffd3a8e7b7aa5321a to your computer and use it in GitHub Desktop.
Save Coneboy-k/898ffd3a8e7b7aa5321a to your computer and use it in GitHub Desktop.
#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