@synchronized vs dispatch_once
来源:互联网 发布:手机qq视频录像软件 编辑:程序博客网 时间:2024/05/21 04:42
From: http://bjhomer.blogspot.jp/2011/09/synchronized-vs-dispatchonce.html
@synchronized vs dispatch_once
In the comments on a recent Stack Overflow question, someone asked me if there was a significant performance difference between @synchronized and dispatch_once in implementing a singleton. So I wrote a simple test harness to access a singleton using the @synchronized method shown here:
@synchronized(self) { if (!synchronizedVar) { synchronizedVar = [[Test alloc] init]; }}return synchronizedVar;
and the dispatch_once method shown here:
static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{ dispatchVar = [[Test alloc] init];});return dispatchVar;
Each test accessed the singleton object 10 million times. I ran both single-threaded tests and multi-threaded tests. Here were the results:
Single threaded results----------------------- @synchronized: 3.3829 seconds dispatch_once: 0.9891 secondsMulti threaded results---------------------- @synchronized: 33.5171 seconds dispatch_once: 1.6648 seconds
So yeah, dispatch_once is a lot faster, especially under thread contention. You can find my test harness on github.
- @synchronized vs dispatch_once
- dispatch_once和@synchronized的单例模式
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- IOS-dispatch_once和@synchronized的单例区别
- iOS UI篇 - @synchronized与dispatch_once 创建单例
- 细说@synchronized和dispatch_once[细读][笔记]
- Synchronized vs SyncRoot
- synchronized. VS ReentrantLock.
- synchronized(this) VS synchronized( other object)
- synchronized VS Lock, wait-notify VS Condition
- C# Hashtable Synchronized vs SyncRoot
- C# Hashtable Synchronized vs SyncRoot
- C# Hashtable Synchronized vs SyncRoot
- 黑马程序员_javaEE写一个简单的servlet
- Jquery和prototype冲突问题
- NSTimerは基本的にretainせずassignでよい
- 求三个数中的最大数 (分别考虑整数、双精度数、长整数)【函数的重载】
- 电容参数:X5R,X7R,Y5V,COG 详解
- @synchronized vs dispatch_once
- 函数模板】求两个整数 或者三个整数中的最大数 自动判断你输入的是几个整数
- AudioServicesPlaySystemSound音频服务—IOS开发
- You have tried to change the API from what has been previously approved
- Oracle order by 处理NULL值
- 【Oracle】树状结构查询
- iOS多线程编程指南(四)线程同步
- Objective-C基础教程学习笔记 内存管理
- 关于raw os 的强实时性