high-performance-go-workshop icon indicating copy to clipboard operation
high-performance-go-workshop copied to clipboard

Inconsistent example about Fibonacci benchmarks

Open nawa opened this issue 7 years ago • 0 comments

There is something wrong with the story about Fibonacci benchmarks presented in the compiled view here https://dave.cheney.net/high-performance-go-workshop/dotgo-paris.htm

First, you have the example of an unoptimized Fibonacci algorithm

func Fib(n int) int {
	switch n {
	case 0:
		return 0
	case 1:
		return 1
	case 2:
		return 2
	default:
		return Fib(n-1) + Fib(n-2)
	}
}

Despite it is wrong and provides incorrect result maybe it's made intentionally. But then you have a slightly optimized version on the paragraph 2.3.1. Improve Fib

func Fib(n int) int {
	switch n {
	case 0:
		return 0
	case 1:
		return 1
	case 2:
		return 1
	default:
		return Fib(n-1) + Fib(n-2)
	}
}

It's a correct working version of the algorithm but they are equal from the performance point of view, so the point about some optimization looks strange

nawa avatar Apr 29 '19 15:04 nawa