RosettaCodeData/Task/Time-a-function/Go/time-a-function-3.go

32 lines
630 B
Go
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
package main
import (
"fmt"
2016-12-05 22:15:40 +01:00
"testing"
2013-04-11 01:07:29 -07:00
)
2016-12-05 22:15:40 +01:00
func empty() {}
2013-04-11 01:07:29 -07:00
func count() {
for i := 0; i < 1e6; i++ {
}
}
func main() {
2016-12-05 22:15:40 +01:00
e := testing.Benchmark(func(b *testing.B) {
for i := 0; i < b.N; i++ {
empty()
}
})
c := testing.Benchmark(func(b *testing.B) {
for i := 0; i < b.N; i++ {
count()
}
})
fmt.Println("Empty function: ", e)
fmt.Println("Count to a million:", c)
fmt.Println()
fmt.Printf("Empty: %12.4f\n", float64(e.T.Nanoseconds())/float64(e.N))
fmt.Printf("Count: %12.4f\n", float64(c.T.Nanoseconds())/float64(c.N))
2013-04-11 01:07:29 -07:00
}