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 - 互斥锁&&自旋锁 多线程安全隐患(转载)
一.多线程安全隐患 资源共享 一块资源可能会被多个线程共享,也就是多个线程可能会访问到一块资源 比如多个线程访问同一个对象,同一个变量,同一个文件. 当多线程访问同一块资源的时候,很容易引发数据错乱 ...
随机推荐
- C#设计模式(5)——建造者模式
一.概念 建造者模式将一个复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的表示. 二.模型 三.代码实现 /// <summary> /// 建造者模式的演变 /// 省略了指挥 ...
- Crypto Challenge Set 1解题报告
1.Convert hex to base64 题意:给出一个hex编码过的字符串,将它进行base64加密 解题关键:直接利用base64库函数实现 import base64 str1=" ...
- Linux 下安装配置 JDK7(2)
Linux 下安装配置 JDK7 自从从Oracle收购Sun近三年来,已经有很多变化.早在8月,甲骨文将"Operating System Distributor License for ...
- HDU - 1546 ZOJ - 2750 Idiomatic Phrases Game 成语接龙SPFA+map
Idiomatic Phrases Game Tom is playing a game called Idiomatic Phrases Game. An idiom consists of sev ...
- Python开发【第四篇】:运算符
1. 算术运算符 算术运算符包括+.-.*./.%.//.**,即加减乘除,取余,取商(地板除法)以及幂运算. >>> 5 + 2 7 >>> 5 - 2 ...
- Sharepoint2013搜索学习笔记之设置业务数据内容源(六)
Sharepoint搜索爬网组件支持爬Business Data Connectivity Service 承载的外部数据,关于Business Data Connectivity Service设置 ...
- 脚本执行JavaScript代码
下面是一个具体的demo测试脚本引擎,执行javas文件. package Rhino; import java.io.FileReader;import java.net.URL; import j ...
- yii2之目录解析
/backend 1.assets\AppAsset.php2.config\main-local.php 注释14到17行3.controllers\SiteController -> act ...
- [Lintcode]Word Squares(DFS|字符串)
题意 略 分析 0.如果直接暴力1000^5会TLE,因此考虑剪枝 1.如果当前需要插入第i个单词,其剪枝如下 1.1 其前缀(0~i-1)已经知道,必定在前缀对应的集合中找 – 第一个词填了ball ...
- centos7及以上安装git服务
检查git是否安装或者是版本 whereis git等命令来检查是否已经安装了git版本的 git --version检测到我的环境自带的git版本 已安装但不是想要的版本需要卸载 yum remov ...