Experiences with go profiling mechanism

This commit is contained in:
Balhau 2025-01-02 17:50:53 +00:00
commit 96b52a1583
5 changed files with 72 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
*.prof
*.gif
*.pdf
*.balhau

18
README.md Normal file
View file

@ -0,0 +1,18 @@
# Profiling
```
# Compile it
go build
# Test it
go test
# Profile CPU
go test -cpuprofile cpu.prof -bench .
# Visualize profiled data
go tool pprof cpu.prof
```

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module goprofile.balhau
go 1.18

28
main.go Normal file
View file

@ -0,0 +1,28 @@
package main
func Fib2(n int) uint64 {
if n == 0 {
return 0
} else if n == 1 {
return 1
} else {
return Fib2(n-1) + Fib2(n-2)
}
}
func Fib(n int) *uint64 {
//var f [100010]uint64
//f := make([]uint64, 10000)
var f [10000]uint64
f[0] = 0
f[1] = 1
for i := 2; i <= n; i++ {
f[i] = f[i-1] + f[i-2]
}
return &f[n]
}
func main() {
}

19
main_test.go Normal file
View file

@ -0,0 +1,19 @@
package main
import "testing"
func TestGetVal(t *testing.T) {
for i := 0; i < 1000; i++ {
if Fib2(30) != 832040 {
t.Error("Incorrect!")
}
}
}
func TestGetValOpt(t *testing.T) {
for i := 0; i < 1000; i++ {
if Fib(30) != 832040 {
t.Error("Incorrect!")
}
}
}