2
Hello, I’m a beginner in Go and I’m having a hard time determining how much memory the Go code took to execute some code. I made the memory() function that even returns something, but I don’t know how it would be to bring the return I want, it would be like: the code needed 15MB to be executed, for example, would it have to do this? Thank you in advance!
package main
import (
"fmt"
"log"
"runtime"
"time"
)
func ack(m, n uint) uint {
if m == 0 {
return n + 1
} else if n == 0 {
return ack(m - 1, 1)
} else {
return ack(m - 1, ack(m, n - 1))
}
}
func memory(){
for {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Printf("\nAlloc = %v\nTotalAlloc = %v\nSys = %v\nNumGC = %v\n\n", m.Alloc / 1024, m.TotalAlloc / 1024, m.Sys / 1024, m.NumGC)
time.Sleep(5 * time.Second)
}
}
func main() {
var inicio = time.Now().UnixNano()
fmt.Println(ack(1,2))
var fim = time.Now().UnixNano()
fmt.Println(fim - inicio)
memory()
}
You need stack, heap, or time?
– Ainar-G
I need the total memory that was used to execute the code
– Henrique Buzin