Last active
December 4, 2020 10:06
-
-
Save koher/84dba57a42d788b6208017106f75a5b1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
func measure(_ body: () -> Void) { | |
let start = Date.timeIntervalSinceReferenceDate | |
for _ in 0 ..< 10 { | |
body() | |
} | |
let end = Date.timeIntervalSinceReferenceDate | |
print((end - start) / 10) | |
} | |
func sumOfArray(_ a: [Int], indices: [Int]) -> Int { | |
var sum = 0 | |
for i in indices { | |
sum &+= a[i] | |
} | |
return sum | |
} | |
func sumOfArraySlice(_ a: ArraySlice<Int>, indices: [Int]) -> Int { | |
var sum = 0 | |
for i in indices { | |
sum &+= a[i] | |
} | |
return sum | |
} | |
let n: Int = 1_000_000 + (1 ... 100).randomElement()! | |
let a: [Int] = .init(1 ... n) | |
let b: ArraySlice<Int> = .init(a) | |
let indices: [Int] = a.indices.shuffled() | |
var sum = 0 | |
measure { sum &+= sumOfArray(a, indices: indices) } | |
measure { sum &+= sumOfArraySlice(b, indices: indices) } | |
print(sum) |
This file contains hidden or 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 | |
func measure(_ body: () -> Void) { | |
let start = Date.timeIntervalSinceReferenceDate | |
for _ in 0 ..< 10 { | |
body() | |
} | |
let end = Date.timeIntervalSinceReferenceDate | |
print((end - start) / 10) | |
} | |
func mapFilter(_ n: Int) -> [Int] { | |
precondition(n >= 1) | |
return (1 ... n).map { $0 * $0 }.filter { $0 % 7 == 1 } | |
} | |
func lazyMapFilter(_ n: Int) -> [Int] { | |
precondition(n >= 1) | |
return Array((1 ... n).lazy.map { $0 * $0 }.filter { $0 % 7 == 1 }) | |
} | |
func mapFilterByLoop(_ n: Int) -> [Int] { | |
precondition(n >= 1) | |
var result: [Int] = [] | |
for i in 1 ... n { | |
let square = i * i | |
guard square % 7 == 1 else { continue } | |
result.append(square) | |
} | |
return result | |
} | |
let n: Int = 10_000_000 + (1 ... 100).randomElement()! | |
var a = 0 | |
measure { a += mapFilter(n).count } | |
measure { a += lazyMapFilter(n).count } | |
measure { a += mapFilterByLoop(n).count } | |
print(a) |
This file contains hidden or 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 | |
func measure(_ body: () -> Void) { | |
let start = Date.timeIntervalSinceReferenceDate | |
for _ in 0 ..< 10 { | |
body() | |
} | |
let end = Date.timeIntervalSinceReferenceDate | |
print((end - start) / 10) | |
} | |
public func sumOfEvensByStride(_ n: Int) -> Int { | |
var sum = 0 | |
for m in stride(from: 0, through: n * 2, by: 2) { | |
sum &+= m | |
} | |
return sum | |
} | |
public func sumOfEvensByRange(_ n: Int) -> Int { | |
var sum = 0 | |
for m in 0 ... n { | |
sum &+= m * 2 | |
} | |
return sum | |
} | |
public func sumOfMultiplesByStride(_ n: Int, k: Int) -> Int { | |
var sum = 0 | |
for m in stride(from: 0, through: n * k, by: k) { | |
sum &+= m | |
} | |
return sum | |
} | |
public func sumOfMultiplesByRange(_ n: Int, k: Int) -> Int { | |
var sum = 0 | |
for m in 0 ... n { | |
sum &+= m * k | |
} | |
return sum | |
} | |
let n = 100_000_000 + (1 ... 100).randomElement()! | |
var a = 0 | |
measure { a &+= sumOfEvensByStride(n) } | |
measure { a &+= sumOfEvensByRange(n) } | |
measure { a &+= sumOfMultiplesByStride(n, k: 3) } | |
measure { a &+= sumOfMultiplesByRange(n, k: 3) } | |
print(a) |
This file contains hidden or 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 | |
func measure(_ body: () -> Void) { | |
let start = Date.timeIntervalSinceReferenceDate | |
for _ in 0 ..< 10 { | |
body() | |
} | |
let end = Date.timeIntervalSinceReferenceDate | |
print((end - start) / 10) | |
} | |
extension Array { | |
mutating func mySwapAt(_ i: Int, _ j: Int) { | |
let t = self[i] | |
self[i] = self[j] | |
self[j] = t | |
} | |
} | |
func swapBySwapAt(_ array: inout [Int]) { | |
for i in array.indices.dropFirst() { | |
array.swapAt(i - 1, i) | |
} | |
} | |
func swapByT(_ array: inout [Int]) { | |
for i in array.indices.dropFirst() { | |
let t = array[i] | |
array[i] = array[i - 1] | |
array[i - 1] = t | |
} | |
} | |
func swapByMySwapAt(_ array: inout [Int]) { | |
for i in array.indices.dropFirst() { | |
array.mySwapAt(i - 1, i) | |
} | |
} | |
let n: Int = 10_000_000 + (1 ... 100).randomElement()! | |
var a: [Int] = Array(1 ... n) | |
measure { swapBySwapAt(&a) } | |
measure { swapByT(&a) } | |
measure { swapByMySwapAt(&a) } | |
print(a.count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment