Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 12 of 60 · Go

Benchmarking & Profiling

Duration: 15 minutes

Benchmarking

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

Benchmarks live in _test.go files and use the *testing.B argument.

func BenchmarkFib(b *testing.B) {
    for i := 0; i < b.N; i++ { fib(20) }
}

Run with the -bench flag:

$ go test -bench=.

Profiling CPU & Memory

$ go test -cpuprofile=cpu.out -memprofile=mem.out ./...
$ go tool pprof cpu.out

Inside the pprof REPL you can type top, list Fib, web (requires Graphviz).

Race Detector

Detect data races in concurrent code:

$ go run -race main.go

Info

Profiling works best on compiled binaries (go build) rather than go run.

Previous: Testing in Go: The `testing` PackageNext: Concurrency Primer: Goroutines