Performance and Benchmarking Basics
Operation Counts
Performance starts with asking how much work a loop performs as input grows.
Operation Counts
operation_counts.go
package main
import "fmt"
func main() {
var items =
visits := 0
total := 0
for i := 1; i <= items; i++ {
visits++
total += i
}
fmt.Println("items=", items)
fmt.Println("visits=", visits)
fmt.Println("total=", total)
fmt.Println("linear=", visits == items)
}
package main
import "fmt"
func main() {
var items =
visits := 0
total := 0
for i := 1; i <= items; i++ {
visits++
total += i
}
fmt.Println("items=", items)
fmt.Println("visits=", visits)
fmt.Println("total=", total)
fmt.Println("linear=", visits == items)
}
package main
import "fmt"
func main() {
var items =
visits := 0
total := 0
for i := 1; i <= items; i++ {
visits++
total += i
}
fmt.Println("items=", items)
fmt.Println("visits=", visits)
fmt.Println("total=", total)
fmt.Println("linear=", visits == items)
}
operation count
Counting simple operations is deterministic and helps explain performance before measuring real time.