let fibonacci = memoize { x, op in x < 2 ? x : op(x-1) + op(x-2) }
let fibonacciSparse = sparseMemoize(when: { $0 % 3 != 0 }) { x, op in x < 2 ? x : op(x-1) + op(x-2) }
let fib10 = fibonacci(10)
let fibSparse10 = fibonacciSparse(10)
Performance Test:
var totalFibTime = 0.0
var totalSpaTime = 0.0
func test(_ count: Int) {
let fibTime = measure {
for _ in 0..<count {
let fib = memoize { x, op in x < 2 ? x : op(x-1) + op(x-2) }
_ = fib(90)
}
}
let spaTime = measure {
for _ in 0..<count {
let sparseFib = sparseMemoize(when: { ($0 + 1) % 3 != 0 }) { x, op in x < 2 ? x : op(x-1) + op(x-2) }
_ = sparseFib(90)
}
}
totalFibTime += fibTime
totalSpaTime += spaTime
print("Regular:", fibTime, "\tSparse:", spaTime, "\tRatio (regular/sparse):", fibTime / spaTime)
}
func measure(_ f: () -> ()) -> Double {
let start = Date()
f()
return -start.timeIntervalSinceNow
}
for _ in 0..<10 {
test(50_000)
}
print("TOTAL - Regular:", totalFibTime, "- Sparse:", totalSpaTime)
results in
Regular: 1.18163102865219 Sparse: 1.44286596775055 Ratio (regular/sparse): 0.818947189179583
Regular: 1.19898700714111 Sparse: 1.45090401172638 Ratio (regular/sparse): 0.826372384010767
Regular: 1.15271997451782 Sparse: 1.42640602588654 Ratio (regular/sparse): 0.808128929349823
Regular: 1.12670803070068 Sparse: 1.40854495763779 Ratio (regular/sparse): 0.799909171937429
Regular: 1.1258339881897 Sparse: 1.40661597251892 Ratio (regular/sparse): 0.800384760435779
Regular: 1.19773703813553 Sparse: 1.40450495481491 Ratio (regular/sparse): 0.852782351553448
Regular: 1.13253104686737 Sparse: 1.40245896577835 Ratio (regular/sparse): 0.807532394531648
Regular: 1.15904998779297 Sparse: 1.43797796964645 Ratio (regular/sparse): 0.806027638989446
Regular: 1.15362799167633 Sparse: 1.41204297542572 Ratio (regular/sparse): 0.816992125419214
Regular: 1.1202210187912 Sparse: 1.40167397260666 Ratio (regular/sparse): 0.799202268633091
TOTAL - Regular: 11.5490471124649 - Sparse: 14.1939957737923