iOS 8种锁的性能实际探索

1、os_unfair_lock


//
//  HHLockViewController.m
//  MideaTechnologyStack
//
//  Created by huocy on 2021/9/3.
//
#import "HHLockViewController.h"
#import 
#import 
#import 
#import 

@interface HHLockViewController ()
///os_unfair_lock
@property (nonatomic)os_unfair_lock unfair_lock;
@property (nonatomic,strong) NSLock *lock;
///信号量
@property (nonatomic) dispatch_semaphore_t semaphore;
@property (nonatomic) pthread_mutex_t mutex;
@property (nonatomic,strong) NSMutableArray *times;
@end
  
@implementation HHLockViewController
{
    NSMutableArray *_synchronizedArray;
    NSMutableArray *_lockArray;
    NSMutableArray *_os_unfair_lockArray;
    NSMutableArray *_semaphoreArray;
    NSMutableArray *_pthreadArray;
    NSMutableArray *_mutexArray;
    NSMutableArray *_conditionLockArray;
    NSMutableArray *_recursiveLockArray;
    NSMutableArray *_spinLockUnlockArray;
}

- (instancetype)init{
    if (self = [super init]) {
        _synchronizedArray = [NSMutableArray array];
        _lockArray = [NSMutableArray array];
        _os_unfair_lockArray = [NSMutableArray array];
        _semaphoreArray = [NSMutableArray array];
        _pthreadArray = [NSMutableArray array];
        _mutexArray = [NSMutableArray array];
        _conditionLockArray = [NSMutableArray array];
        _recursiveLockArray = [NSMutableArray array];
        _spinLockUnlockArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    //信号量初始化
    self.semaphore = dispatch_semaphore_create(1);
    //苹果废弃的自旋锁   
    loopsLock = OS_SPINLOCK_INIT;
    //苹果替代原有废弃的自旋锁
    self.unfair_lock = OS_UNFAIR_LOCK_INIT;
    //NSLock
    self.lock = [[NSLock alloc] init];
    //phtread
    pthread_mutex_init (&_mutex, NULL);
   
    //1、 os_unfair_lock
    [self os_unfair_lock];
    
    //2、NS_lock
    [self NS_lock];
  
    //3、信号量
    [self dispatch_semaphore_t];
  
    //4 、synchronized
    [self synchronized];
    
    //5、 p_thread
    [self p_thread];
    
    //6 、OSSpinLock
    [self OSSPinLockS];
       
}

///os_unfair_lock
-(void)os_unfair_lock{
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            [self taskNS_os_unfair_lock];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s1  时间 == %f ms",_cmd,linkTime);
    });
    
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            [self taskNS_os_unfair_lock];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s2  时间 == %f ms",_cmd,linkTime);
    });
}

- (HHLockModel *)taskNS_os_unfair_lock{
    os_unfair_lock_lock(&_unfair_lock);
    HHLockModel *model = [[HHLockModel alloc] init];
    model.lockName = @"taskNS_os_unfair_lock";
    [_os_unfair_lockArray addObject:model];
    os_unfair_lock_unlock(&_unfair_lock);
    return model;
}
  

打印结果

2021-09-06 18:37:23.878622+0800 [57682:24014145] os_unfair_lock1  时间 == 0.000302 ms
2021-09-06 18:37:23.878689+0800 [57682:24014334] os_unfair_lock2  时间 == 0.000311 ms

2、NSLock

///lock
-(void)NS_lock{
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            [self taskNS_lock];
       }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s1  时间 == %f ms",_cmd,linkTime);
    });
     dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            [self taskNS_lock];
         }
         CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
         NSLog(@"%s2  时间 == %f ms",_cmd,linkTime);
     });
}

- (HHLockModel *)taskNS_lock{
    [self.lock lock];
    HHLockModel *model = [[HHLockModel alloc] init];
    model.lockName = @"NSLock";
    [_lockArray addObject:model];
    [self.lock unlock];
    return model;
}
   

打印结果

2021-09-06 18:37:23.878446+0800 [57682:24014330] NS_lock1  时间 == 0.000142 ms
2021-09-06 18:37:23.878495+0800 [57682:24014331] NS_lock2  时间 == 0.000186 ms

3、dispatch_semaphore_t

///GCD信号量
-(void)dispatch_semaphore_t{
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            [self taskNS_dispatch_semaphore_signal];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s1  时间 == %f ms",_cmd,linkTime);
    });
    
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            [self taskNS_dispatch_semaphore_signal];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s2  时间 == %f ms",_cmd,linkTime);
    });
}

- (HHLockModel *)taskNS_dispatch_semaphore_signal{
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
    HHLockModel *model = [[HHLockModel alloc] init];
    model.lockName = @"dispatch_semaphore_t";
    [_semaphoreArray addObject:model];
    dispatch_semaphore_signal(self.semaphore);
    return model;
}
   

打印结果

2021-09-06 18:37:23.878551+0800 [57682:24014332] dispatch_semaphore_t1  时间 == 0.000235 ms
2021-09-06 18:37:23.878604+0800 [57682:24014333] dispatch_semaphore_t2  时间 == 0.000261 mss

4、synchronized

//@synchronized
-(void)synchronized{
    
    NSMutableArray *array = [NSMutableArray array];
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            id obj = [self taskSynchronized];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s1  时间 == %f ms",_cmd,linkTime);
        _times[3] = @(linkTime *1000000);
        [barChart updateChartData:_times];
    });
    
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            id obj = [self taskSynchronized];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s2  时间 == %f ms",_cmd,linkTime);
    });
}

- (HHLockModel *)taskSynchronized{
    @synchronized (self) {
        HHLockModel *model = [[HHLockModel alloc] init];
        model.lockName = @"@synchronized";
        [_synchronizedArray addObject:model];
        return model;
    }
}

打印结果

2021-09-06 18:37:23.878376+0800 [57682:24014322] synchronized1  时间 == 0.000083 ms
2021-09-06 18:37:23.878396+0800 [57682:24014145] synchronized2  时间 == 0.000112 ms

5、p_thread

-(void)p_thread{
    NSMutableArray *array = [NSMutableArray array];
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            [self taskNS_pthreadLock];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s1  时间 == %f ms",_cmd,linkTime);
        _times[4] = @(linkTime * 1000000);
        dispatch_async(dispatch_get_main_queue(), ^{
            [barChart updateChartData:_times];
        });
    });
    
    dispatch_async(dispatch_get_global_queue(0,0), ^{
         for (int i = 0 ; i<100; i++) {
            [self taskNS_pthreadLock];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s2  时间 == %f ms",_cmd,linkTime);
    });
}


- (HHLockModel *)taskNS_pthreadLock{
    pthread_mutex_lock(&_mutex); /*获取互斥锁*/
    HHLockModel *model = [[HHLockModel alloc] init];
    model.lockName = @"p_thread";
    pthread_mutex_unlock(&_mutex);
    return model;
}

打印结果

2021-09-06 18:37:23.878653+0800 [57682:24014330] p_thread2  时间 == 0.000327 ms
2021-09-06 18:37:23.878667+0800 [57682:24014331] p_thread1  时间 == 0.000332 ms

6、OSSPinLock

-(void)OSSPinLockS{
    NSMutableArray *array = [NSMutableArray array];
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        for (int i = 0 ; i<100; i++) {
            [self taskNS_OSSPinLockS];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s1  时间 == %f ms",_cmd,linkTime);
    });
    
    dispatch_async(dispatch_get_global_queue(0,0), ^{
         for (int i = 0 ; i<100; i++) {
            [self taskNS_OSSPinLockS];
        }
        CFAbsoluteTime linkTime =  (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"%s2  时间 == %f ms",_cmd,linkTime);
    });
}


- (HHLockModel *)taskNS_OSSPinLockS{
    OSSpinLockLock(&loopsLock);
    HHLockModel *model = [[HHLockModel alloc] init];
    model.lockName = @"OSSPinLockS";
    [_os_unfair_lockArray addObject:model];
    OSSpinLockUnlock(&loopsLock);
    return model;
}

打印结果

2021-09-06 18:37:23.878775+0800 [57682:24014332] OSSpinLockUnlock2 OSSPinLockS  时间 == 0.000432 ms
2021-09-06 18:37:23.878775+0800 [57682:24014337] OSSpinLockUnlock1 OSSPinLockS  时间 == 0.000423 ms

end 总结


2021-09-06 18:37:23.878376+0800 [57682:24014322] synchronized1  时间 == 0.000083 ms
2021-09-06 18:37:23.878396+0800 [57682:24014145] synchronized2  时间 == 0.000112 ms
2021-09-06 18:37:23.878446+0800 [57682:24014330] NS_lock1  时间 == 0.000142 ms
2021-09-06 18:37:23.878495+0800 [57682:24014331] NS_lock2  时间 == 0.000186 ms
2021-09-06 18:37:23.878551+0800 [57682:24014332] dispatch_semaphore_t1  时间 == 0.000235 ms
2021-09-06 18:37:23.878604+0800 [57682:24014333] dispatch_semaphore_t2  时间 == 0.000261 ms
2021-09-06 18:37:23.878622+0800 [57682:24014145] os_unfair_lock1  时间 == 0.000302 ms
2021-09-06 18:37:23.878653+0800 [57682:24014330] p_thread2  时间 == 0.000327 ms
2021-09-06 18:37:23.878667+0800 [57682:24014331] p_thread1  时间 == 0.000332 ms
2021-09-06 18:37:23.878689+0800 [57682:24014334] os_unfair_lock2  时间 == 0.000311 ms
2021-09-06 18:37:23.878775+0800 [57682:24014332] OSSpinLockUnlock2 OSSPinLockS  时间 == 0.000432 ms
2021-09-06 18:37:23.878775+0800 [57682:24014337] OSSpinLockUnlock1 OSSPinLockS  时间 == 0.000423 ms

你可能感兴趣的:(iOS 8种锁的性能实际探索)