Regarding time.NewTicker() and monotonic time

392 views
Skip to first unread message

Curtis Paul

unread,
Jun 10, 2020, 6:48:00 PM6/10/20
to golang-nuts
It sounds like NewTicker will dynamically adjust to keep tick time "accurate".

Does anyone know if the time data that NewTicker returns (i.e. via it's channel, etc...) includes monotonic time?
And if so is the definition of NewTicker referring to adjusting real time clock or monotonic clock?

I'm not really sure what to expect with using ticker in terms of timing accuracy.
Is NewTicker() monotonic?

Also not quite sure what "Stop the ticker to release associated resources" refers to.

time.NewTicker()

"NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources."

andrey mirtchovski

unread,
Jun 10, 2020, 6:50:52 PM6/10/20
to Curtis Paul, golang-nuts
> Does anyone know if the time data that NewTicker returns (i.e. via it's channel, etc...) includes monotonic time?

it's right at the top: https://golang.org/pkg/time/
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ec9cf4f2-5404-4512-9b57-b4816ea47adeo%40googlegroups.com.

Ian Lance Taylor

unread,
Jun 10, 2020, 7:04:24 PM6/10/20
to Curtis Paul, golang-nuts
On Wed, Jun 10, 2020 at 3:48 PM Curtis Paul <curti...@gmail.com> wrote:
>
> It sounds like NewTicker will dynamically adjust to keep tick time "accurate".
>
> Does anyone know if the time data that NewTicker returns (i.e. via it's channel, etc...) includes monotonic time?
> And if so is the definition of NewTicker referring to adjusting real time clock or monotonic clock?
>
> I'm not really sure what to expect with using ticker in terms of timing accuracy.
> Is NewTicker() monotonic?

Tickers use the monotonic clock.


> Also not quite sure what "Stop the ticker to release associated resources" refers to.
>
> time.NewTicker()
>
> "NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources."

In the current implementations of Go, Tickers are not garbage
collected. They run until they are stopped. So if you don't stop a
ticker, it will keep ticking until your program exits.

(It is possible that future implementations will garbage collect
Tickers, but it still won't hurt to stop a ticker that you no longer
need.)

Ian

Curtis Paul

unread,
Jun 10, 2020, 7:24:03 PM6/10/20
to golang-nuts
Ya, I didn't see anything specific in that part of the doc regarding NewTicker and monotonic.  I did see that some things in time. do not consider monotonic clock.
Then the blurb on NewTicker didn't say anything about monotonic.

I guess it's safe to assume NewTicker returns a monotonic clock time and that time would be evaluated by the functions that work with monotonic.
Just wondering if anyone has had experience or knows something more specific about it.

On Wednesday, June 10, 2020 at 4:50:52 PM UTC-6, andrey mirtchovski wrote:
> Does anyone know if the time data that NewTicker returns (i.e. via it's channel, etc...) includes monotonic time?

it's right at the top: https://golang.org/pkg/time/

On Wed, Jun 10, 2020 at 4:48 PM Curtis Paul <curti...@gmail.com> wrote:
>
> It sounds like NewTicker will dynamically adjust to keep tick time "accurate".
>
> Does anyone know if the time data that NewTicker returns (i.e. via it's channel, etc...) includes monotonic time?
> And if so is the definition of NewTicker referring to adjusting real time clock or monotonic clock?
>
> I'm not really sure what to expect with using ticker in terms of timing accuracy.
> Is NewTicker() monotonic?
>
> Also not quite sure what "Stop the ticker to release associated resources" refers to.
>
> time.NewTicker()
>
> "NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources."
>
> --
> 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 golan...@googlegroups.com.

Curtis Paul

unread,
Jun 10, 2020, 7:25:43 PM6/10/20
to golang-nuts
Cool, makes sense.  Assuming NewTicker does return monotonic time.

I wonder if there is a way to verify.

andrey mirtchovski

unread,
Jun 10, 2020, 7:34:23 PM6/10/20
to Curtis Paul, golang-nuts
> Cool, makes sense. Assuming NewTicker does return monotonic time.
>
> I wonder if there is a way to verify.

just fmt.Println the value you receive on the ticker chan, you'll see
the monotonic component tacked on in the end:

$ cat t.go
package main
import (
"fmt"
"time"
)
func main() {
t := time.NewTicker(time.Second)
fmt.Println(<-t.C)
}
$ go run t.go
2020-06-10 17:31:04.234797 -0600 MDT m=+1.003493724
$

Curtis Paul

unread,
Jun 10, 2020, 7:51:02 PM6/10/20
to golang-nuts
It sure does...thanks again :)

Konstantin Khomoutov

unread,
Jun 17, 2020, 10:24:44 AM6/17/20
to golang-nuts
On Wed, Jun 10, 2020 at 04:03:36PM -0700, Ian Lance Taylor wrote:

[...]
> In the current implementations of Go, Tickers are not garbage
> collected. They run until they are stopped. So if you don't stop a
> ticker, it will keep ticking until your program exits.
>
> (It is possible that future implementations will garbage collect
> Tickers, but it still won't hurt to stop a ticker that you no longer
> need.)

Do I understand correctly, that a stopped Tiker is not garbage
collected, too? IOW, stopping a Ticker merely makes it not waste CPU
time but it will still float in memory.

Ian Lance Taylor

unread,
Jun 17, 2020, 9:07:21 PM6/17/20
to Konstantin Khomoutov, golang-nuts
A stopped Ticker will be garbage collected.

Ian
Reply all
Reply to author
Forward
0 new messages