Created
February 26, 2017 09:28
-
-
Save andersio/0e852ce9166c798e2ea3b4a3579803ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @testable import ReactiveSwift | |
| import XCTest | |
| class AtomicTests: XCTestCase { | |
| let iterations = 10000000 | |
| func test() { | |
| var info = mach_timebase_info() | |
| mach_timebase_info(&info) | |
| let lock = UnsafeUnfairLock() | |
| var avg1: Double = 0 | |
| for _ in 0 ..< iterations { | |
| let s = mach_absolute_time() | |
| lock.lock() | |
| lock.unlock() | |
| let e = mach_absolute_time() | |
| avg1 += Double(e - s) / Double(info.denom) * Double(info.numer) / Double(iterations) | |
| } | |
| lock.destroy() | |
| print("UnsafeUnfairLock: \(avg1) ns") | |
| } | |
| func test2() { | |
| var info = mach_timebase_info() | |
| mach_timebase_info(&info) | |
| let lock = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1) | |
| lock.initialize(to: pthread_mutex_t()) | |
| pthread_mutex_init(lock, nil) | |
| var avg1: Double = 0 | |
| for _ in 0 ..< iterations { | |
| let s = mach_absolute_time() | |
| pthread_mutex_lock(lock) | |
| pthread_mutex_unlock(lock) | |
| let e = mach_absolute_time() | |
| avg1 += Double(e - s) / Double(info.denom) * Double(info.numer) / Double(iterations) | |
| } | |
| lock.deinitialize() | |
| lock.deallocate(capacity: 1) | |
| print("pthread_mutex: \(avg1) ns") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment