commit 96b52a158330a79bae56c20f41d01bcb5d24a52e Author: Balhau Date: Thu Jan 2 17:50:53 2025 +0000 Experiences with go profiling mechanism diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63c7d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.prof +*.gif +*.pdf +*.balhau diff --git a/README.md b/README.md new file mode 100644 index 0000000..8f6f22d --- /dev/null +++ b/README.md @@ -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 + +``` + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cef5a39 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module goprofile.balhau + +go 1.18 diff --git a/main.go b/main.go new file mode 100644 index 0000000..782c1be --- /dev/null +++ b/main.go @@ -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() { + +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..a637cd6 --- /dev/null +++ b/main_test.go @@ -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!") + } + } +}