/
ViewController.m
211 lines (178 loc) · 6.59 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// ViewController.m
// iOSLockBenckmark
//
// Created by ibireme on 16/1/14.
// Copyright © 2016 ibireme. All rights reserved.
//
#import "ViewController.h"
#import <pthread.h>
#import <libkern/OSAtomic.h>
#import <QuartzCore/QuartzCore.h>
typedef NS_ENUM(NSUInteger, LockType) {
LockTypeOSSpinLock = 0,
LockTypedispatch_semaphore,
LockTypepthread_mutex,
LockTypeNSCondition,
LockTypeNSLock,
LockTypepthread_mutex_recursive,
LockTypeNSRecursiveLock,
LockTypeNSConditionLock,
LockTypesynchronized,
LockTypeCount,
};
NSTimeInterval TimeCosts[LockTypeCount] = {0};
int TimeCount = 0;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
int buttonCount = 5;
for (int i = 0; i < buttonCount; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200, 50);
button.center = CGPointMake(self.view.frame.size.width / 2, i * 60 + 160);
button.tag = pow(10, i + 3);
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:@"run (%d)",(int)button.tag] forState:UIControlStateNormal];
[button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
- (void)tap:(UIButton *)sender {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self test:(int)sender.tag];
});
}
- (IBAction)clear:(id)sender {
for (NSUInteger i = 0; i < LockTypeCount; i++) {
TimeCosts[i] = 0;
}
TimeCount = 0;
printf("---- clear ----\n\n");
}
- (IBAction)log:(id)sender {
printf("OSSpinLock: %8.2f ms\n", TimeCosts[LockTypeOSSpinLock] * 1000);
printf("dispatch_semaphore: %8.2f ms\n", TimeCosts[LockTypedispatch_semaphore] * 1000);
printf("pthread_mutex: %8.2f ms\n", TimeCosts[LockTypepthread_mutex] * 1000);
printf("NSCondition: %8.2f ms\n", TimeCosts[LockTypeNSCondition] * 1000);
printf("NSLock: %8.2f ms\n", TimeCosts[LockTypeNSLock] * 1000);
printf("pthread_mutex(recursive): %8.2f ms\n", TimeCosts[LockTypepthread_mutex_recursive] * 1000);
printf("NSRecursiveLock: %8.2f ms\n", TimeCosts[LockTypeNSRecursiveLock] * 1000);
printf("NSConditionLock: %8.2f ms\n", TimeCosts[LockTypeNSConditionLock] * 1000);
printf("@synchronized: %8.2f ms\n", TimeCosts[LockTypesynchronized] * 1000);
printf("---- fin (sum:%d) ----\n\n",TimeCount);
}
- (void)test:(int)count {
NSTimeInterval begin, end;
TimeCount += count;
{
OSSpinLock lock = OS_SPINLOCK_INIT;
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
OSSpinLockLock(&lock);
OSSpinLockUnlock(&lock);
}
end = CACurrentMediaTime();
TimeCosts[LockTypeOSSpinLock] += end - begin;
printf("OSSpinLock: %8.2f ms\n", (end - begin) * 1000);
}
{
dispatch_semaphore_t lock = dispatch_semaphore_create(1);
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
dispatch_semaphore_signal(lock);
}
end = CACurrentMediaTime();
TimeCosts[LockTypedispatch_semaphore] += end - begin;
printf("dispatch_semaphore: %8.2f ms\n", (end - begin) * 1000);
}
{
pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
pthread_mutex_lock(&lock);
pthread_mutex_unlock(&lock);
}
end = CACurrentMediaTime();
TimeCosts[LockTypepthread_mutex] += end - begin;
pthread_mutex_destroy(&lock);
printf("pthread_mutex: %8.2f ms\n", (end - begin) * 1000);
}
{
NSCondition *lock = [NSCondition new];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
[lock lock];
[lock unlock];
}
end = CACurrentMediaTime();
TimeCosts[LockTypeNSCondition] += end - begin;
printf("NSCondition: %8.2f ms\n", (end - begin) * 1000);
}
{
NSLock *lock = [NSLock new];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
[lock lock];
[lock unlock];
}
end = CACurrentMediaTime();
TimeCosts[LockTypeNSLock] += end - begin;
printf("NSLock: %8.2f ms\n", (end - begin) * 1000);
}
{
pthread_mutex_t lock;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&lock, &attr);
pthread_mutexattr_destroy(&attr);
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
pthread_mutex_lock(&lock);
pthread_mutex_unlock(&lock);
}
end = CACurrentMediaTime();
TimeCosts[LockTypepthread_mutex_recursive] += end - begin;
pthread_mutex_destroy(&lock);
printf("pthread_mutex(recursive): %8.2f ms\n", (end - begin) * 1000);
}
{
NSRecursiveLock *lock = [NSRecursiveLock new];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
[lock lock];
[lock unlock];
}
end = CACurrentMediaTime();
TimeCosts[LockTypeNSRecursiveLock] += end - begin;
printf("NSRecursiveLock: %8.2f ms\n", (end - begin) * 1000);
}
{
NSConditionLock *lock = [[NSConditionLock alloc] initWithCondition:1];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
[lock lock];
[lock unlock];
}
end = CACurrentMediaTime();
TimeCosts[LockTypeNSConditionLock] += end - begin;
printf("NSConditionLock: %8.2f ms\n", (end - begin) * 1000);
}
{
NSObject *lock = [NSObject new];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
@synchronized(lock) {}
}
end = CACurrentMediaTime();
TimeCosts[LockTypesynchronized] += end - begin;
printf("@synchronized: %8.2f ms\n", (end - begin) * 1000);
}
printf("---- fin (%d) ----\n\n",count);
}
@end