func BenchmarkSort(b *testing.B) { for i := 0; i < b.N; i++ { b.StartTimer() ints := []int{5, 4, 1, 3, 2} sort.Ints(ints) b.StopTimer() } }
func BenchmarkFibonacci(b *testing.B) { for i := 0; i < b.N; i++ { b.StartTimer() fib(30) b.StopTimer() } } func fib(n int) int { if n < 2 { return n } return fib(n-1) + fib(n-2) }In this example, the test function computes the 30th number in the Fibonacci sequence using a recursive function. The StartTimer function is called before calling the fib function, and the StopTimer function is called after. This ensures that the time taken to compute the Fibonacci number is measured accurately. Overall, the Go testing package is a powerful tool for writing tests and benchmarks in Go programs. StartTimer is just one of many functions available in the package, and it can be used to measure the time taken to execute specific parts of code.