Measuring Function Runtime in Go
In Go, a simple way to measure the execution time of a function is to leverage the defer feature.
Implementation
func trace(s string) (string, time.Time) { log.Println("START:", s) return s, time.Now() } func un(s string, startTime time.Time) { endTime := time.Now() log.Println(" END:", s, "ElapsedTime in seconds:", endTime.Sub(startTime)) }
func someFunction() { defer un(trace("SOME_ARBITRARY_STRING_SO_YOU_CAN_KEEP_TRACK")) // Perform the function's intended operations here... }
Explanation
Note:
The above is the detailed content of How to Measure Function Runtime in Go Using the Defer Feature?. For more information, please follow other related articles on the PHP Chinese website!