func fibonacci2(n uint64) uint64 {
f0, f1 := uint64(0), uint64(1)
for i := uint64(2); i <= n; i++ {
f0, f1 = f1, f0+f1
}
return f1
}
func fibonacci3(n uint64) uint64 {
var stack Stack
for j := n - 1; j >= 2; j-- {
stack.push(j)
}
f0, f1 := uint64(0), uint64(1)
for _, ok := stack.pop(); ok; _, ok = stack.pop() {
f0, f1 = f1, f0+f1
}
return f1
}
go test -test.bench=".*"
N = 45
testing: warning: no tests to run
Benchmark_Recursive-4110018573100 ns/op
Benchmark_Loop-420000000000.00 ns/op
Benchmark_Stack-420000000000.00 ns/op
PASS
ok github.com/vipally/glab/lab2 10.154s