1. 程式人生 > >3.10 計算代碼運行的時長

3.10 計算代碼運行的時長

waitgroup AC code OS pack down RM mark new


package main

import (
    "fmt"
    "sync"
    "time"
)

func main() {

    t := time.NewTimer(3 * time.Second)
    fmt.Printf("Start waiting at %v\n", time.Now().Format(time.UnixDate))
    <-t.C
    fmt.Printf("Code executed at %v\n", time.Now().Format(time.UnixDate))

    wg := &sync.WaitGroup{}
    wg.Add(1
) fmt.Printf("Start waiting for AfterFunc at %v\n", time.Now().Format(time.UnixDate)) time.AfterFunc(3*time.Second, func() { fmt.Printf("Code executed for AfterFunc at %v\n", time.Now().Format(time.UnixDate)) wg.Done() }) wg.Wait() fmt.Printf("Waiting on time.After at %v
\n", time.Now().Format(time.UnixDate)) <-time.After(3 * time.Second) fmt.Printf("Code resumed at %v\n", time.Now().Format(time.UnixDate)) } /* Start waiting at Thu Mar 22 00:32:42 CST 2018 Code executed at Thu Mar 22 00:32:45 CST 2018 Start waiting for AfterFunc at Thu Mar 22 00:32:45 CST 2018 Code executed for AfterFunc at Thu Mar 22 00:32:48 CST 2018
Waiting on time.After at Thu Mar 22 00:32:48 CST 2018 Code resumed at Thu Mar 22 00:32:51 CST 2018 */

3.10 計算代碼運行的時長