Skip to content

Commit

Permalink
fix #126: optimize fibonacci number algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Feb 17, 2019
1 parent fb1db0d commit c7badc5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ generate:

.PHONY: test
test: #| Runs tests with race.
go test -race ./...
go test -race -timeout 1s ./...

.PHONY: test-check
test-check: #| Fast runs tests to check their compilation errors.
go test -run=^hack ./...

.PHONY: test-with-coverage
test-with-coverage: #| Runs tests with coverage.
go test -cover ./...
go test -cover -timeout 1s ./...

.PHONY: test-with-coverage-formatted
test-with-coverage-formatted: #| Runs tests with coverage and formats the result.
go test -cover ./... | column -t | sort -r
go test -cover -timeout 1s ./... | column -t | sort -r

.PHONY: test-with-coverage-profile
test-with-coverage-profile: #| Runs tests with coverage and collects the result.
go test -covermode count -coverprofile cover.out ./...
go test -covermode count -coverprofile cover.out -timeout 1s ./...

.PHONY: test-example
test-example: #| Runs example tests with coverage and collects the result.
go test -covermode count -coverprofile -run=Example -v example.out ./...
go test -covermode count -coverprofile -run=Example -timeout 1s -v example.out ./...
11 changes: 6 additions & 5 deletions backoff/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ func Fibonacci(factor time.Duration) Algorithm {
// fibonacciNumber calculates the Fibonacci sequence number for the given
// sequence position.
func fibonacciNumber(n uint) uint {
if 0 == n {
if n == 0 {
return 0
} else if 1 == n {
return 1
} else {
return fibonacciNumber(n-1) + fibonacciNumber(n-2)
}
var a, b uint = 0, 1
for i := uint(1); i < n; i++ {
a, b = b, a+b
}
return b
}
4 changes: 4 additions & 0 deletions backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ func TestFibonacci(t *testing.T) {
t.Errorf("algorithm expected to return a %s duration, but received %s instead", expected, result)
}
}

t.Run("performance", func(t *testing.T) {
_ = Fibonacci(time.Millisecond)(50)
})
}

0 comments on commit c7badc5

Please sign in to comment.