AppTest
AppTest

Reputation: 501

Equivalent of python time.time()-started in golang

In python I can see how many seconds have elapsed during a specific process like,

started = time.time()
doProcess()
print(time.time()-started)

Whats the equivelent in golang?

Upvotes: 0

Views: 1341

Answers (3)

peterSO
peterSO

Reputation: 166626

Package time

func Since

func Since(t Time) Duration

Since returns the time elapsed since t. It is shorthand for time.Now().Sub(t).

Your Python example in Go:

package main

import (
    "fmt"
    "time"
)

func main() {
    started := time.Now()
    time.Sleep(1 * time.Second)
    fmt.Println(time.Since(started))
}

Output:

1s

Upvotes: 1

iamareebjamal
iamareebjamal

Reputation: 321

import (
    "fmt"
    "time"
)

func main() {
    started := time.Now()
    doProcess()
    fmt.Println(time.Now().Sub(started).Seconds())
}

Upvotes: 2

Abhilekh Singh
Abhilekh Singh

Reputation: 2953

import (
    "fmt"
    "time"
)

func main() {
    begin := time.Now()
    time.Sleep(10 * time.Millisecond)
    end := time.Now()

    duration := end.Sub(begin)

    fmt.Println(duration)

}

Upvotes: 2

Related Questions