I am running a GO (1.9.2) program 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)))
(The two fmt statements are on consecutive lines)
I expected that the printout would be of similar time but here are some results of the print:
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 is consistently high between the two printouts.
What could be the explanation of this phenomenon?
Extra info: According to pprof there are ~15000 goroutines running at the time of the prints but most of them are waiting for incoming data on sockets.
I ran the code with GODEBUG=gctrace=1
but there aren't many GC printouts, not nearly as many as the number of printouts of my code.
EDIT: It seems that storing the result of time.Since() into variables as suggested by @Verran solves the issue.
Changing to fmt
to log
didn't help but the prints are no longer synchronized.
It appears the "problem" is in the way fmt
is handled in a high load environment. I hope someone could shed some light to what is going on here.