Created
April 30, 2017 01:13
-
-
Save TheIronBorn/cc0507b36fddad291aa34674eb52ad8f 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
import Foundation | |
private let measurements = 1_000 | |
private func measureExecutionTime(_ title: String, f: (() -> Any) ) { | |
let start = Date() | |
var result: Any? | |
for _ in 0..<measurements { | |
result = f() | |
} | |
let end = Date() | |
let duration = end.timeIntervalSince(start) | |
print("\(title.padding(toLength: 10, withPad: " ", startingAt: 0)) | \("\(duration)".padding(toLength: 21, withPad: " ", startingAt: 0)) | \(result ?? "OHNO")") | |
} | |
/********************* start setup *********************/ | |
private extension MutableCollection where Index == Int, IndexDistance == Int { | |
mutating func shuffle() { | |
guard count > 1 else { return } | |
let random: (Int) -> Int = { Int(arc4random_uniform(UInt32($0))) } | |
for i in 0..<count - 1 { | |
let j = random(count - i) + i | |
guard i != j else { continue } | |
swap(&self[i], &self[j]) | |
} | |
} | |
} | |
private let input = ["zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","twentyone","twentytwo","twentythree","twentyfour","twentyfive","twentysix","twentyseven","twentyeight","twentynine","thirty","thirtyone","thirtytwo","thirtythree","thirtyfour","thirtyfive","thirtysix","thirtyseven","thirtyeight","thirtynine","forty","fortyone","fortytwo","fortythree","fortyfour","fortyfive","fortysix"] | |
private let rand = { input[Int(arc4random_uniform(UInt32(input.count)))] } | |
private var arr: [String] = [] | |
for i in 0..<1000 { | |
arr.append(rand()) | |
} | |
arr.shuffle() | |
/********************* start measuring *********************/ | |
measureExecutionTime("orig") { | |
func minimumMaximum<T: Comparable>(_ array: [T]) -> (minimum: T, maximum: T)? { | |
var array = array | |
guard !array.isEmpty else { | |
return nil | |
} | |
var minimum = array.first! | |
var maximum = array.first! | |
let hasOddNumberOfItems = array.count % 2 != 0 | |
if hasOddNumberOfItems { | |
array.removeFirst() | |
} | |
while !array.isEmpty { | |
let pair = (array.removeFirst(), array.removeFirst()) | |
if pair.0 > pair.1 { | |
if pair.0 > maximum { | |
maximum = pair.0 | |
} | |
if pair.1 < minimum { | |
minimum = pair.1 | |
} | |
} else { | |
if pair.1 > maximum { | |
maximum = pair.1 | |
} | |
if pair.0 < minimum { | |
minimum = pair.0 | |
} | |
} | |
} | |
return (minimum, maximum) | |
} | |
return minimumMaximum(arr)! | |
} | |
measureExecutionTime("stride") { | |
func minimumMaximum<T: Comparable>(_ array: [T]) -> (minimum: T, maximum: T)? { | |
guard !array.isEmpty else { | |
return nil | |
} | |
var minimum = array.first! | |
var maximum = array.first! | |
// if 'array' has an odd number of items, let 'minimum' or 'maximum' deal with the leftover | |
let hasOddNumberOfItems = array.count % 2 != 0 | |
let start = hasOddNumberOfItems ? 1 : 0 | |
for i in stride(from: start, to: array.count, by: 2) { | |
let pair = (array[i], array[i+1]) | |
if pair.0 > pair.1 { | |
if pair.0 > maximum { | |
maximum = pair.0 | |
} | |
if pair.1 < minimum { | |
minimum = pair.1 | |
} | |
} else { | |
if pair.1 > maximum { | |
maximum = pair.1 | |
} | |
if pair.0 < minimum { | |
minimum = pair.0 | |
} | |
} | |
} | |
return (minimum, maximum) | |
} | |
return minimumMaximum(arr)! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example output: