Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:01
Show Gist options
  • Save up1/2e12846bc90afd5dd238 to your computer and use it in GitHub Desktop.
Save up1/2e12846bc90afd5dd238 to your computer and use it in GitHub Desktop.
Go :: Benchmark
package demo
func Fibonacci(number int) int {
if number < 2 {
return number
}
return Fibonacci(number-1) + Fibonacci(number-2)
}
package demo
import "testing"
func BenchmarkFibonacci5(b *testing.B) {
for n := 0; n < b.N; n++ {
Fibonacci(5)
}
}
package demo
import "testing"
func benchmarkFibonacci(number int, b *testing.B) {
for n := 0; n < b.N; n++ {
Fibonacci(number)
}
}
func BenchmarkFibonacci1(b *testing.B) {
benchmarkFibonacci(1, b)
}
func BenchmarkFibonacci3(b *testing.B) {
benchmarkFibonacci(3, b)
}
func BenchmarkFibonacci5(b *testing.B) {
benchmarkFibonacci(5, b)
}
func BenchmarkFibonacci7(b *testing.B) {
benchmarkFibonacci(7, b)
}
func BenchmarkFibonacci10(b *testing.B) {
benchmarkFibonacci(10, b)
}
func BenchmarkFibonacci20(b *testing.B) {
benchmarkFibonacci(20, b)
}
func BenchmarkFibonacci30(b *testing.B) {
benchmarkFibonacci(30, b)
}
func BenchmarkFibonacci40(b *testing.B) {
benchmarkFibonacci(40, b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment