Strange behavior of time. Because the()

I run the GO program (1.9.2), and I have code similar to:

startTime := time.Now()
...
...
fmt.Printf("%v (1) %v \n", user.uid, int64(time.Since(startTime)))
fmt.Printf("%v (F) %v \n", user.uid, int64(time.Since(startTime)))

(Two fmt statements are on consecutive lines)

I expected the printout to be similar to time, but here are some print results:

921 (1)    2000100
921 (F) 3040173800

(3 seconds)

360 (1)    2000100
360 (F) 1063060800

(1 second)

447 (1)    4000200
447 (F) 2564146700

(2.5 seconds)

The time difference between the two printouts is always high.

What could be the explanation for this phenomenon?

Additional information: According to pprof, at the time of printing ~ 15,000 goroutines are working, but most of them are waiting for incoming data in sockets.

I ran the code with GODEBUG=gctrace=1, but not many GC prints, and not as many as the number of prints of my code.

EDIT: It seems to save the result of time. Since () in the variables suggested by @Verran solves the problem.

fmt log , .

, "" , fmt . , - , .

+4
2

, . , mem stats, GC, GC . startTime , 100% , goroutine.

0

( ), , , . ,

package main

import (
    "fmt"
    "time"
)

func main() {
    var user_uid string
    startTime := time.Now()
    since := time.Since(startTime)
    fmt.Printf("%v (1) %v %v\n", user_uid, int64(since), since)
    since = time.Since(startTime)
    fmt.Printf("%v (F) %v %v\n", user_uid, int64(since), since)
}

:

 (1) 142 142ns
 (F) 22036 22.036ยตs

time.Since fmt.Printf , goroutines.

0

Source: https://habr.com/ru/post/1690609/


All Articles