class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .brown
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
test_OSSpinLock()
test_dispatch_semaphore_t()
test_os_unfair_lock_lock()
test_pthread_mutex_t()
test_NSlock()
test_NSCondition()
test_PTHREAD_MUTEX_RECURSIVE()
test_NSRecursiveLock()
test_NSConditionLock()
test_synchronized()
}
let excuteTimes = 100_000
func test_OSSpinLock() {
var lock = OS_SPINLOCK_INIT
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
OSSpinLockLock(&lock)
OSSpinLockUnlock(&lock)
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_OSSpinLock: \(deta)ms")
}
func test_dispatch_semaphore_t() {
let lock = DispatchSemaphore(value: 1)
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
_ = lock.wait(timeout: .distantFuture)
lock.signal()
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_dispatch_semaphore_t: \(deta)ms")
}
func test_os_unfair_lock_lock() {
var lock = os_unfair_lock.init()
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
os_unfair_lock_lock(&lock)
os_unfair_lock_unlock(&lock)
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_os_unfair_lock_lock: \(deta)ms")
}
func test_pthread_mutex_t() {
var lock = pthread_mutex_t()
pthread_mutex_init(&lock, nil)
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
pthread_mutex_lock(&lock)
pthread_mutex_unlock(&lock)
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_pthread_mutex_t: \(deta)ms")
}
func test_NSlock() {
let lock = NSLock()
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
lock.lock()
lock.unlock()
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_NSlock: \(deta)ms")
}
func test_NSCondition() {
let lock = NSCondition()
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
lock.lock()
lock.unlock()
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_NSCondition: \(deta)ms")
}
func test_PTHREAD_MUTEX_RECURSIVE() {
var mutext_recurive = pthread_mutex_t()
var attr = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)
pthread_mutex_init(&mutext_recurive, &attr)
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
pthread_mutex_lock(&mutext_recurive)
pthread_mutex_unlock(&mutext_recurive)
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_PTHREAD_MUTEX_RECURSIVE: \(deta)ms")
}
func test_NSRecursiveLock() {
let lock = NSRecursiveLock()
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
lock.lock()
lock.unlock()
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_NSRecursiveLock: \(deta)ms")
}
func test_NSConditionLock() {
let lock = NSConditionLock()
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
lock.lock()
lock.unlock()
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_NSConditionLock: \(deta)ms")
}
func test_synchronized() {
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<excuteTimes {
// Swift 的 @synchronized
objc_sync_enter(self)
objc_sync_exit(self)
}
let end = CFAbsoluteTimeGetCurrent()
let deta = (end - start) * 1000
print("test_synchronized: \(deta)ms")
}
}