On 1/31/22, envee <neeraj.vaidy...@gmail.com> wrote: > Hi All, > I understand this issue has been discussed in the past, and I have seen a > few comments from Ian and Russ Cox about this topic, but I could not arrive > > at a conclusion about how I can make the time.Ticker send me ticks at > atleast close to the Ticker duration I specify. At the moment, I am seeing > ticks being sent at way over the specified duration especially when I have > sub-millisecond durations. > With 1ms duration, the ticker seems to be quite close to the duration > (maybe within +/-5%). I would be happier if it were within 1%, but I can > handle that. > > With 1 micro-second duration, the ticker sends ticks nearly 4 times slower > than expected. > > Here is my sample code > " > ti := time.NewTicker(1 * time.Millisecond) > start := time.Now() > for i := 0; i < 1000; i++ { > <-ti.C > } > fmt.Printf("elapsed time = %v\n", time.Since(start)) > " > > The output is usually close to 1 second as expected (close to) when using > 1ms duration. > elapsed time = 1.000159338s > > But when I change the Ticker duration to 1 micro-second, I get the > following output which shows the elapsed time close to 4 seconds: > elapsed time = 4.796662856s > *Is there anyway, I can ensure ticks are sent at approximately within 5% of > > my specified duration ?* > > I understand from time.Ticker docs that ticks will be lost if the ticker > handler cannot keep up with the ticker time. > > In the simple code above, I am literally doing very minimal work in each > loop iteration and that is about checking the loop condition. > So I am not sure why ticks are being lost ? The answer probably depends on what you are interested in: Why ~2000 cycles is not enough for scheduling the loop or could ~2000 cycles be enough for scheduling the loop? What do you think should happen if you change time.Microsecond to time.Nanosecond?
-- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CA%2BctqrrDVq4HFgXOMdOV%2Bfuu1svr6yAXFcDOFeYhXhRc2x_PRg%40mail.gmail.com.