I was doing pressure testing in my project and found a large gap in the time interval due to time.Sleep, which I abstracted to the following code: code 1:
func TestSleep(t *testing.T) {
for i := 0; i < 8; i++ {
//time.Sleep(time.Second)
t1 := time.Now()
fmt.Println( time.Since(t1))
}
}
output:
229ns
37ns
36ns
37ns
36ns
38ns
37ns
39ns
code 2(add time.Sleep):
func TestSleep(t *testing.T) {
for i := 0; i < 8; i++ {
time.Sleep(time.Second)
t1 := time.Now()
fmt.Println( time.Since(t1))
}
}
471ns
267ns
855ns
484ns
359ns
296ns
324ns
302ns
I have two questions:
- Why is there such a big difference between the two results (only the time.Sleep difference)?
- Why is the first output in code1 much larger than the next?