ios 各种锁的使用性能比较
iOS开发中常用的锁有如下几种
来比较一下遇到加锁的情况:
1. @synchronized 关键字加锁
2. NSLock 对象锁
3. NSCondition
4. NSConditionLock 条件锁
5. NSRecursiveLock 递归锁
6. pthread_mutex 互斥锁(C语言)
7. dispatch_semaphore 信号量实现加锁(GCD)
8. OSSpinLock (暂不建议使用,原因参见这里)
//分别使用8种方式加锁 解锁1千万次
- (void)runLock{
CFTimeInterval timeBefore;
CFTimeInterval timeCurrent;
NSUInteger i;
NSUInteger count = 1000*10000;//执行一千万次
//@synchronized
id obj = [[NSObjectalloc]init];;
timeBefore = CFAbsoluteTimeGetCurrent();
for(i=0; i<count; i++){
@synchronized(obj){
}
}
timeCurrent = CFAbsoluteTimeGetCurrent();
printf("@synchronized used : %f\n", timeCurrent-timeBefore);
//NSLock
NSLock *lock = [[NSLockalloc]init];
timeBefore = CFAbsoluteTimeGetCurrent();
for(i=0; i<count; i++){
[lock lock];
[lock unlock];
}
timeCurrent = CFAbsoluteTimeGetCurrent();
printf("NSLock used : %f\n", timeCurrent-timeBefore);
//NSCondition
NSCondition *condition = [[NSConditionalloc]init];
timeBefore = CFAbsoluteTimeGetCurrent();
for(i=0; i<count; i++){
[condition lock];
[condition unlock];
}
timeCurrent = CFAbsoluteTimeGetCurrent();
printf("NSCondition used : %f\n", timeCurrent-timeBefore);
//NSConditionLock
NSConditionLock *conditionLock = [[NSConditionLockalloc]init];
timeBefore = CFAbsoluteTimeGetCurrent();
for(i=0; i<count; i++){
[conditionLock lock];
[conditionLock unlock];
}
timeCurrent = CFAbsoluteTimeGetCurrent();
printf("NSConditionLock used : %f\n", timeCurrent-timeBefore);
//NSRecursiveLock
NSRecursiveLock *recursiveLock = [[NSRecursiveLockalloc]init];
timeBefore = CFAbsoluteTimeGetCurrent();
for(i=0; i<count; i++){
[recursiveLock lock];
[recursiveLock unlock];
}
timeCurrent = CFAbsoluteTimeGetCurrent();
printf("NSRecursiveLock used : %f\n", timeCurrent-timeBefore);
//pthread_mutex
pthread_mutex_t mutex =PTHREAD_MUTEX_INITIALIZER;
timeBefore = CFAbsoluteTimeGetCurrent();
for(i=0; i<count; i++){
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
}
timeCurrent = CFAbsoluteTimeGetCurrent();
printf("pthread_mutex used : %f\n", timeCurrent-timeBefore);
//dispatch_semaphore
dispatch_semaphore_t semaphore =dispatch_semaphore_create(1);
timeBefore = CFAbsoluteTimeGetCurrent();
for(i=0; i<count; i++){
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
dispatch_semaphore_signal(semaphore);
}
timeCurrent = CFAbsoluteTimeGetCurrent();
printf("dispatch_semaphore used : %f\n", timeCurrent-timeBefore);
//OSSpinLockLock
OSSpinLock spinlock = OS_SPINLOCK_INIT;
timeBefore = CFAbsoluteTimeGetCurrent();
for(i=0; i<count; i++){
OSSpinLockLock(&spinlock);
OSSpinLockUnlock(&spinlock);
}
timeCurrent = CFAbsoluteTimeGetCurrent();
printf("OSSpinLock used : %f\n", timeCurrent-timeBefore);
}
输出结果如下:
由图可以发现:
OSSpinLock的性能最好(不建议使用),GCD的dispatch_semaphore紧随其后;
NSConditionLock和@synchronized性能较差;
注意:
1. 需要注意的是这里仅仅是对各种锁直接Lock和Unlock的性能测试,其中部分锁的使用条件上还是有细微的差异的,比如NSLock之类的还有tryLock等方法用于加锁,不同对象锁的功能偏向不一样等等,有兴趣的可以逐个搜索再更深入的研究不同锁之间的区别。
2. 另外,一般来说客户端很少会有这么大量的加锁解锁操作,所以日常来说这些锁的性能都是可以满足使用需求的。
ios 各种锁的使用性能比较的更多相关文章
- Delphi IOS 蓝牙锁屏后台运行
Delphi IOS 后台运行 同样的程序,编译成android,锁屏后继续运行正常,蓝牙通讯正常,但在IOS下锁屏后程序的蓝牙就中断通讯了? IOS的机制就是这样,锁屏就关闭了. 音乐播放器是怎么做 ...
- iOS数据锁
简介 当一个线程访问数据时,而其他数据不能进行访问,保证线程安全或者可以理解为执行多线程,对于共享资源访问时保证互斥的要求 文章 不再安全的 OSSpinLock iOS开发中的11种锁以及性能对比 ...
- iOS 线程锁同步机制
转载自 http://yulingtianxia.com/blog/2015/11/01/More-than-you-want-to-know-about-synchronized/ 如果你已经使用 ...
- Delphi IOS MusicPlayer 锁屏运行学习
[weak] FMusicPlayer: TMusicPlayer; [weak]修饰, 编译器在处理这个变量的时候不会调用该变量内容的__ObjAddRef和__ObjRelease., proce ...
- 不会吧,这也行?iOS后台锁屏监听摇一摇
目录 背景介绍 探索过程 其他 APP 有没有类似功能 系统提供的摇一摇回调能否满足 其他方法能否实现 利用 CoreMotion 框架,监听加速计原始数据 通过加速计监听摇一摇 控制器相关逻辑和代码 ...
- iOS - Mac 锁屏快捷键设置
Mac 锁屏快捷键设置 control + shift + Eject 锁屏快捷键 如果用户要离开电脑一段时间,可以选择直接把笔记本直接合上.但是这样原先在跑的进程就会挂起或者结束,如果正在下载,那么 ...
- iOS开发——锁屏监听
公司所做的项目,锁屏监听是为了60秒后,解锁瓶后显示[手势解锁]或[指纹验证]: 第一步:AppDelegate.m 头部导入 #import <notify.h> #define Not ...
- iOS图案锁,支持动画、图片、绘图
最近忙着搭建一个聊天用的框架,过几天应该会整理写出来吧,原理不难,但是实现后会省很多事.好久没写博客,周末心血来潮写了个图案锁,这东西没什么技术含量,网上一堆,这次这个图案锁顺便联系了怎么打包使用.a ...
- iOS - 互斥锁&&自旋锁 多线程安全隐患(转载)
一.多线程安全隐患 资源共享 一块资源可能会被多个线程共享,也就是多个线程可能会访问到一块资源 比如多个线程访问同一个对象,同一个变量,同一个文件. 当多线程访问同一块资源的时候,很容易引发数据错乱 ...
随机推荐
- NSCLIENT++可以采集的指标
Documentation Information Commands/Modules CheckDisk CheckFileSize CheckDriveSize CheckF ...
- POJ-3050
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4385 Accepted: 2924 Descrip ...
- opencvsharp BitmapSource图片截取问题
private BitmapSource GetUiImage(FrameworkElement ui) { RenderTargetBitmap bmp=new RenderTargetBitmap ...
- WSAData是个什么结构体用什么用
WSAData机构体如下 功能是:存放windows socket初始化信息.struct WSAData { WORD wVersion; WORD wHighVersion; char szDes ...
- 无监督学习:Deep Auto-encoder(深度自动编码器)
一 Auto-encoder NN Encoder & NN Decoder 要一起训练. 二 Starting from PCA 三 Deep Auto-encoder PCA&De ...
- jenkins 找不到mvn 命令
错误如下: /data/jenkins/temp/hudson9132559581388971644.sh: line 4: mvn: command not found 方法如下: 1 修改环境变 ...
- 2014-10-31 NOIP模拟赛
10.30 NOIp 模拟赛 时间 空间 测试点 评测方式 挖掘机(dig.*) 1s 256M 10 传统 黑红树(brtree.*) 2s 256M 10 传统 藏宝图(treas. ...
- 洛谷P3080 [USACO13MAR]牛跑The Cow Run
P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...
- Vue实现任务列表效果
<!DOCTYPE html> <html lang="en"> <head> ...
- Centos7中给gitLab汉化
第一步:安装git 下载补西,东西在清华大学开源中心 yum install –y git 第二步:下载 git clone https://gitlab.com/xhang/gitlab.git 第 ...