Chapter 1 doesn't use concurrency to improve performance
Opened this issue · 0 comments
brackendawson commented
In chapter 1's Fibonacci examples we're told we can use memoization and concurrency to improve the performance of our recursive pure function.
The concurrency example does use concurrency to separate execution of the algorithm from yielding the result, but the algorithm is in no way concurrent or parallel. It is a sequential loop algorithm running on a single separate goroutine.
This is a condensed version of it without the channel:
func FibUnchanneled(n int) int {
n1, n2 := 0, 1
for i := 0; i < n; i++ {
n1, n2 = n2, n1+n2
}
return n1
}
Just removing the "concurrency" like this radically improves the performance compared to the "channeled" approach:
goos: darwin
goarch: amd64
pkg: github.com/l3x/learn-fp-go/1-functional-fundamentals/ch01-pure-fp/02_fib/src/fibonacci
cpu: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
BenchmarkFibChanneled-12 549099 2263 ns/op 96 B/op 1 allocs/op
BenchmarkFibUnchenneled-12 389374429 3.048 ns/op 0 B/op 0 allocs/op
BenchmarkFibMemoized-12 210407667 5.685 ns/op 0 B/op 0 allocs/op
BenchmarkFibSimple-12 15622911 79.00 ns/op 0 B/op 0 allocs/op
PASS
coverage: 100.0% of statements
ok github.com/l3x/learn-fp-go/1-functional-fundamentals/ch01-pure-fp/02_fib/src/fibonacci 53.838s