Skip to content

Instantly share code, notes, and snippets.

@stollcri
Created June 6, 2014 15:13
Show Gist options
  • Save stollcri/4908e837aa9fb2e3c123 to your computer and use it in GitHub Desktop.
Save stollcri/4908e837aa9fb2e3c123 to your computer and use it in GitHub Desktop.
Minimum Functions for 3 Integers (or Variadic) in Swift
import CoreFoundation
let TEST_ITTERATIONS = 100.0
let TEST_LOOPS = 100_000
func min3a(a: Int, b: Int, c: Int) -> Int {
if a < b {
if a < c {
return a
} else {
return c
}
} else {
if b < c {
return b
} else {
return c
}
}
}
func min3a_generics<T: Comparable>(a: T, b: T, c: T) -> T {
if a < b {
if a < c {
return a
} else {
return c
}
} else {
if b < c {
return b
} else {
return c
}
}
}
func min3a_variadic(possibleValues: Int...) -> Int {
var minValue = Int.max
for possibleValue in possibleValues {
if possibleValue < minValue {
minValue = possibleValue
}
}
return minValue
}
func min3b(a: Int, b: Int, c: Int) -> Int {
var result = a
if b < result {
result = b
}
if c < result {
return c
}
return result
}
func testA() {
var result = 0
for i in 0..TEST_LOOPS {
result = 0
result += min3a(1, 2, 3)
result += min3a(1, 3, 2)
result += min3a(2, 1, 3)
result += min3a(2, 3, 1)
result += min3a(3, 1, 2)
result += min3a(3, 2, 1)
if (result != 6) {
println("Bad min (method A, group 1, loop \(i)")
}
result = 0
result = min3a(Int.max-2, Int.max-1, Int.max)
result = min3a(Int.max-2, Int.max, Int.max-1)
result = min3a(Int.max-1, Int.max-2, Int.max)
result = min3a(Int.max-1, Int.max, Int.max-2)
result = min3a(Int.max, Int.max-2, Int.max-1)
result = min3a(Int.max, Int.max-1, Int.max-2)
if (result != Int.max-2) {
println("Bad min (method A, group 2, loop \(i)")
}
}
}
func testB() {
var result = 0
for i in 0..TEST_LOOPS {
result = 0
result += min3b(1, 2, 3)
result += min3b(1, 3, 2)
result += min3b(2, 1, 3)
result += min3b(2, 3, 1)
result += min3b(3, 1, 2)
result += min3b(3, 2, 1)
if (result != 6) {
println("Bad min (method B, group 1, loop \(i)")
}
result = 0
result = min3b(Int.max-2, Int.max-1, Int.max)
result = min3b(Int.max-2, Int.max, Int.max-1)
result = min3b(Int.max-1, Int.max-2, Int.max)
result = min3b(Int.max-1, Int.max, Int.max-2)
result = min3b(Int.max, Int.max-2, Int.max-1)
result = min3b(Int.max, Int.max-1, Int.max-2)
if (result != Int.max-2) {
println("Bad min (method B, group 2, loop \(i)")
}
}
}
func testC() {
var result = 0
for i in 0..TEST_LOOPS {
result = 0
result += min(1, 2, 3)
result += min(1, 3, 2)
result += min(2, 1, 3)
result += min(2, 3, 1)
result += min(3, 1, 2)
result += min(3, 2, 1)
if (result != 6) {
println("Bad min (method C, group 1, loop \(i)")
}
result = 0
result = min(Int.max-2, Int.max-1, Int.max)
result = min(Int.max-2, Int.max, Int.max-1)
result = min(Int.max-1, Int.max-2, Int.max)
result = min(Int.max-1, Int.max, Int.max-2)
result = min(Int.max, Int.max-2, Int.max-1)
result = min(Int.max, Int.max-1, Int.max-2)
if (result != Int.max-2) {
println("Bad min (method C, group 2, loop \(i)")
}
}
}
var totalTime = 0.0
for i in 0..TEST_ITTERATIONS {
let timeStart = CFAbsoluteTimeGetCurrent()
testA()
let timeStop = CFAbsoluteTimeGetCurrent()
let fullTime = timeStop - timeStart
totalTime += fullTime
println("Method A: \(fullTime) us ")
}
println("A Avg: \(totalTime / TEST_ITTERATIONS) us")
totalTime = 0.0
for i in 0..TEST_ITTERATIONS {
let timeStart = CFAbsoluteTimeGetCurrent()
testB()
let timeStop = CFAbsoluteTimeGetCurrent()
let fullTime = timeStop - timeStart
totalTime += fullTime
println("Method B: \(fullTime) us ")
}
println("B Avg: \(totalTime / TEST_ITTERATIONS) us")
totalTime = 0.0
for i in 0..TEST_ITTERATIONS {
let timeStart = CFAbsoluteTimeGetCurrent()
testC()
let timeStop = CFAbsoluteTimeGetCurrent()
let fullTime = timeStop - timeStart
totalTime += fullTime
println("Method C: \(fullTime) us ")
}
println("C Avg: \(totalTime / TEST_ITTERATIONS) us")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment