使用 Go 測量執行時間 Memo's Blog 2020-01-07 Others Go, Programming 環境 macOS Go 1.13.4 做法新增 main.go 檔: 1234567891011121314151617181920package mainimport ( "log" "time")func main() { // 傳入當前時間,並延遲執行 defer measure(time.Now(), "main function") for index := 0; index < 10000000000; index++ { // }}func measure(start time.Time, name string) { // 印出經過時間 log.Printf("%s took %s", name, time.Since(start))} 執行應用。 1go run main.go 結果: 12020/01/07 18:07:51 main function took 4.96882091s 參考資料 Measuring execution time in Go