Last active
June 15, 2016 07:16
-
-
Save andersio/e586c3d979a296e312f2be3dd07ccd54 to your computer and use it in GitHub Desktop.
This file contains 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
class Atomic<Value> { | |
/// ... | |
/// Atomically modifies the variable. | |
/// | |
/// Returns the old value. | |
public func modify(@noescape action: (inout Value) throws -> Void) rethrows -> Value { | |
return try withValue { value in | |
try action(&_value) | |
return value | |
} | |
} | |
/// Atomically modifies the variable. | |
/// | |
/// Returns the old value. | |
public func _prev_modify(@noescape action: (Value) throws -> Value) rethrows -> Value { | |
return try withValue { value in | |
_value = try action(value) | |
return value | |
} | |
} | |
/// ... | |
} |
This file contains 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
import XCTest | |
class AtomicSpeedTestCase: XCTestCase { | |
func test_ModifyBeforeChange_10000() { | |
let number = Atomic(1) | |
measureBlock { | |
/// 10000 itrs | |
for _ in 1 ... 10000 { | |
number._prev_modify { $0 + 1 } | |
} | |
} | |
} | |
func test_ModifyBeforeChange_500000() { | |
let number = Atomic(1) | |
measureBlock { | |
/// 500000 itrs | |
for _ in 1 ... 500000 { | |
number._prev_modify { $0 + 1 } | |
} | |
} | |
} | |
func test_ModifyBeforeChange_5000000() { | |
let number = Atomic(1) | |
measureBlock { | |
/// 5000000 itrs | |
for _ in 1 ... 5000000 { | |
number._prev_modify { $0 + 1 } | |
} | |
} | |
} | |
func test_ModifyAfterChange_10000() { | |
let number = Atomic(1) | |
measureBlock { | |
for _ in 1 ... 10000 { | |
number.modify { $0 += 1 } | |
} | |
} | |
} | |
func test_ModifyAfterChange_500000() { | |
let number = Atomic(1) | |
measureBlock { | |
for _ in 1 ... 500000 { | |
number.modify { $0 += 1 } | |
} | |
} | |
} | |
func test_ModifyAfterChange_5000000() { | |
let number = Atomic(1) | |
measureBlock { | |
for _ in 1 ... 5000000 { | |
number.modify { $0 += 1 } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment