Skip to content

Instantly share code, notes, and snippets.

@andersio
Last active June 15, 2016 07:16
Show Gist options
  • Save andersio/e586c3d979a296e312f2be3dd07ccd54 to your computer and use it in GitHub Desktop.
Save andersio/e586c3d979a296e312f2be3dd07ccd54 to your computer and use it in GitHub Desktop.
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
}
}
/// ...
}
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