// Chapter 35 example: measuring how long some work takes. // Run with: go run exercises/35/09-measure.go package main import ( "fmt" "time" ) // isPrime is slow on purpose, so that there is something to measure. func isPrime(n int) bool { if n < 2 { return false } for d := 2; d*d <= n; d++ { if n%d == 0 { return false } } return true } func main() { start := time.Now() count := 0 for n := range 2_000_000 { if isPrime(n) { count++ } } elapsed := time.Since(start) fmt.Println("Primes below 2,000,000:", count) fmt.Println("That took", elapsed) fmt.Println("Rounded: ", elapsed.Round(time.Millisecond)) }