Skip to content

Instantly share code, notes, and snippets.

@amomchilov
Created February 18, 2020 15:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amomchilov/299d012dccba375bf15880355684ebed to your computer and use it in GitHub Desktop.
Save amomchilov/299d012dccba375bf15880355684ebed to your computer and use it in GitHub Desktop.
import Foundation
func randomString(_ length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in letters.randomElement()! })
}
func bm<R>(_ title:String, operation: () -> R) {
var totalTime = 0.0
for _ in (0..<10) {
let startTime = CFAbsoluteTimeGetCurrent()
_ = operation()
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
totalTime += timeElapsed
}
let meanTime = totalTime / 1000
print("Mean time for \(title): \(meanTime) s")
}
func method1<T: Hashable>(_ array: Array<T>) -> Array<T> {
return Array(Set(array))
}
func method2<T: Equatable>(_ array: Array<T>) -> Array<T>{
return array
.enumerated()
.filter{ array.firstIndex(of: $0.1) == $0.0 }
.map{ $0.1 }
}
// Alain T.'s answer (adapted)
func method3<T: Hashable>(_ array: Array<T>) -> Array<T> {
var uniqueKeys = Set<T>()
return array.filter{uniqueKeys.insert($0).inserted}
}
func method4<T: Hashable>(_ array: Array<T>) -> Array<T> {
var uniqueKeys = Set<T>()
uniqueKeys.reserveCapacity(array.count)
return array.filter{uniqueKeys.insert($0).inserted}
}
let shortIntList = (0..<100).map{_ in Int.random(in: 0..<100) }
let longIntList = (0..<10000).map{_ in Int.random(in: 0..<10000) }
let longIntListManyRepetitions = (0..<10000).map{_ in Int.random(in: 0..<100) }
let longStringList = (0..<10000).map{_ in randomString(1000)}
let longMegaStringList = (0..<10000).map{_ in randomString(10000)}
print("Finished generated test data")
bm("method1 shortIntList") { method1(shortIntList) }
bm("method2 shortIntList") { method2(shortIntList) }
bm("method3 shortIntList") { method3(shortIntList) }
bm("method4 shortIntList") { method4(shortIntList) }
bm("method1 longIntList") { method1(longIntList) }
bm("method2 longIntList") { method2(longIntList) }
bm("method3 longIntList") { method3(longIntList) }
bm("method4 longIntList") { method4(longIntList) }
bm("method1 longIntListManyRepetitions") { method1(longIntListManyRepetitions) }
bm("method2 longIntListManyRepetitions") { method2(longIntListManyRepetitions) }
bm("method3 longIntListManyRepetitions") { method3(longIntListManyRepetitions) }
bm("method4 longIntListManyRepetitions") { method4(longIntListManyRepetitions) }
bm("method1 longStringList") { method1(longStringList) }
bm("method2 longStringList") { method2(longStringList) }
bm("method3 longStringList") { method3(longStringList) }
bm("method4 longStringList") { method4(longStringList) }
bm("method1 longMegaStringList") { method1(longMegaStringList) }
bm("method2 longMegaStringList") { method2(longMegaStringList) }
bm("method3 longMegaStringList") { method3(longMegaStringList) }
bm("method4 longMegaStringList") { method4(longMegaStringList) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment