FAQ
Hi all,

I've done a little ticker precision test. I want to do something in a 4 Mhz
interval. My test creates a ticker with 250 nsec duration. So I've expected
4 * 10^6 / 250 nsec = 16000 calls, but I got no more then 13000. What's
wrong?

package main

import (
"fmt"
"time"
)

func main() {
t := time.NewTicker(250 * time.Nanosecond)
stop := make(chan bool, 1)
c := 0

go func() {
for {
select {
case <- t.C:
c++
case <- stop:
return
}
}
}()
time.Sleep(1*time.Second)
stop <- true
fmt.Println(c)
}

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Nate at Apr 30, 2014 at 9:52 pm
    I'm getting ~17000 calls:

    desktop:hertz$ go run main.go
    17219
    desktop:hertz$ go build
    desktop:hertz$ ls
    hertz main.go
    desktop:hertz$ ./hertz
    17197
    desktop:hertz$ go version
    go version go1.2.1 linux/amd64

    On Wednesday, April 30, 2014 3:42:24 PM UTC-6, Jens Frederich wrote:

    Hi all,

    I've done a little ticker precision test. I want to do something in a 4
    Mhz interval. My test creates a ticker with 250 nsec duration. So I've
    expected 4 * 10^6 / 250 nsec = 16000 calls, but I got no more then 13000.
    What's wrong?

    package main

    import (
    "fmt"
    "time"
    )

    func main() {
    t := time.NewTicker(250 * time.Nanosecond)
    stop := make(chan bool, 1)
    c := 0

    go func() {
    for {
    select {
    case <- t.C:
    c++
    case <- stop:
    return
    }
    }
    }()
    time.Sleep(1*time.Second)
    stop <- true
    fmt.Println(c)
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Apr 30, 2014 at 9:56 pm
    It is worth nothing that this program has a data race.

    lucky(~/src) % go run -race rr.go
    ==================
    WARNING: DATA RACE
    Read by main goroutine:
       main.main()
           /home/dfc/src/rr.go:25 +0x1c8

    Previous write by goroutine 6:
       main.func·001()
           /home/dfc/src/rr.go:17 +0xcf

    Goroutine 6 (running) created at:
       main.main()
           /home/dfc/src/rr.go:22 +0x174
    ==================
    11859
    Found 1 data race(s)
    exit status 66

    On Thursday, 1 May 2014 07:42:24 UTC+10, Jens Frederich wrote:

    Hi all,

    I've done a little ticker precision test. I want to do something in a 4
    Mhz interval. My test creates a ticker with 250 nsec duration. So I've
    expected 4 * 10^6 / 250 nsec = 16000 calls, but I got no more then 13000.
    What's wrong?

    package main

    import (
    "fmt"
    "time"
    )

    func main() {
    t := time.NewTicker(250 * time.Nanosecond)
    stop := make(chan bool, 1)
    c := 0

    go func() {
    for {
    select {
    case <- t.C:
    c++
    case <- stop:
    return
    }
    }
    }()
    time.Sleep(1*time.Second)
    stop <- true
    fmt.Println(c)
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Apr 30, 2014 at 9:57 pm
    s/nothing/noting/

    time.Ticker is based on time.Sleep, effectively. So the limitations of
    Sleep apply to Ticker as well, specifically Sleep sleeps for _at least_ the
    amount of time specified, but it can sleep for more.
    On Thursday, 1 May 2014 07:55:48 UTC+10, Dave Cheney wrote:

    It is worth nothing that this program has a data race.

    lucky(~/src) % go run -race rr.go
    ==================
    WARNING: DATA RACE
    Read by main goroutine:
    main.main()
    /home/dfc/src/rr.go:25 +0x1c8

    Previous write by goroutine 6:
    main.func·001()
    /home/dfc/src/rr.go:17 +0xcf

    Goroutine 6 (running) created at:
    main.main()
    /home/dfc/src/rr.go:22 +0x174
    ==================
    11859
    Found 1 data race(s)
    exit status 66

    On Thursday, 1 May 2014 07:42:24 UTC+10, Jens Frederich wrote:

    Hi all,

    I've done a little ticker precision test. I want to do something in a 4
    Mhz interval. My test creates a ticker with 250 nsec duration. So I've
    expected 4 * 10^6 / 250 nsec = 16000 calls, but I got no more then 13000.
    What's wrong?

    package main

    import (
    "fmt"
    "time"
    )

    func main() {
    t := time.NewTicker(250 * time.Nanosecond)
    stop := make(chan bool, 1)
    c := 0

    go func() {
    for {
    select {
    case <- t.C:
    c++
    case <- stop:
    return
    }
    }
    }()
    time.Sleep(1*time.Second)
    stop <- true
    fmt.Println(c)
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Péter Szilágyi at Apr 30, 2014 at 10:01 pm
    Btw, why would you expect 16000 calls?

    1 sec / 250 nsec = 4.000.000

    On Thu, May 1, 2014 at 12:57 AM, Dave Cheney wrote:

    s/nothing/noting/

    time.Ticker is based on time.Sleep, effectively. So the limitations of
    Sleep apply to Ticker as well, specifically Sleep sleeps for _at least_ the
    amount of time specified, but it can sleep for more.

    On Thursday, 1 May 2014 07:55:48 UTC+10, Dave Cheney wrote:

    It is worth nothing that this program has a data race.

    lucky(~/src) % go run -race rr.go
    ==================
    WARNING: DATA RACE
    Read by main goroutine:
    main.main()
    /home/dfc/src/rr.go:25 +0x1c8

    Previous write by goroutine 6:
    main.func·001()
    /home/dfc/src/rr.go:17 +0xcf

    Goroutine 6 (running) created at:
    main.main()
    /home/dfc/src/rr.go:22 +0x174
    ==================
    11859
    Found 1 data race(s)
    exit status 66

    On Thursday, 1 May 2014 07:42:24 UTC+10, Jens Frederich wrote:

    Hi all,

    I've done a little ticker precision test. I want to do something in a 4
    Mhz interval. My test creates a ticker with 250 nsec duration. So I've
    expected 4 * 10^6 / 250 nsec = 16000 calls, but I got no more then 13000.
    What's wrong?

    package main

    import (
    "fmt"
    "time"
    )

    func main() {
    t := time.NewTicker(250 * time.Nanosecond)
    stop := make(chan bool, 1)
    c := 0

    go func() {
    for {
    select {
    case <- t.C:
    c++
    case <- stop:
    return
    }
    }
    }()
    time.Sleep(1*time.Second)
    stop <- true
    fmt.Println(c)
    }

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jens Frederich at Apr 30, 2014 at 10:16 pm
    I would expect a high precision timer. So I've configured a 4 Mhz timer. It
    should be raised precise in a 250 nsec raster. That are 16.000 calls in one
    minute. Or I'm wrong?
    On Thursday, May 1, 2014 12:01:04 AM UTC+2, Péter Szilágyi wrote:

    Btw, why would you expect 16000 calls?

    1 sec / 250 nsec = 4.000.000


    On Thu, May 1, 2014 at 12:57 AM, Dave Cheney <da...@cheney.net<javascript:>
    wrote:
    s/nothing/noting/

    time.Ticker is based on time.Sleep, effectively. So the limitations of
    Sleep apply to Ticker as well, specifically Sleep sleeps for _at least_ the
    amount of time specified, but it can sleep for more.

    On Thursday, 1 May 2014 07:55:48 UTC+10, Dave Cheney wrote:

    It is worth nothing that this program has a data race.

    lucky(~/src) % go run -race rr.go
    ==================
    WARNING: DATA RACE
    Read by main goroutine:
    main.main()
    /home/dfc/src/rr.go:25 +0x1c8

    Previous write by goroutine 6:
    main.func·001()
    /home/dfc/src/rr.go:17 +0xcf

    Goroutine 6 (running) created at:
    main.main()
    /home/dfc/src/rr.go:22 +0x174
    ==================
    11859
    Found 1 data race(s)
    exit status 66

    On Thursday, 1 May 2014 07:42:24 UTC+10, Jens Frederich wrote:

    Hi all,

    I've done a little ticker precision test. I want to do something in a 4
    Mhz interval. My test creates a ticker with 250 nsec duration. So I've
    expected 4 * 10^6 / 250 nsec = 16000 calls, but I got no more then 13000.
    What's wrong?

    package main

    import (
    "fmt"
    "time"
    )

    func main() {
    t := time.NewTicker(250 * time.Nanosecond)
    stop := make(chan bool, 1)
    c := 0

    go func() {
    for {
    select {
    case <- t.C:
    c++
    case <- stop:
    return
    }
    }
    }()
    time.Sleep(1*time.Second)
    stop <- true
    fmt.Println(c)
    }

    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jens Frederich at Apr 30, 2014 at 10:20 pm
    Thanks and good to know. Is there any more accurate Go timer solution, like
    Windows QueryPerformanceCounter?
    On Wednesday, April 30, 2014 11:57:23 PM UTC+2, Dave Cheney wrote:

    s/nothing/noting/

    time.Ticker is based on time.Sleep, effectively. So the limitations of
    Sleep apply to Ticker as well, specifically Sleep sleeps for _at least_ the
    amount of time specified, but it can sleep for more.
    On Thursday, 1 May 2014 07:55:48 UTC+10, Dave Cheney wrote:

    It is worth nothing that this program has a data race.

    lucky(~/src) % go run -race rr.go
    ==================
    WARNING: DATA RACE
    Read by main goroutine:
    main.main()
    /home/dfc/src/rr.go:25 +0x1c8

    Previous write by goroutine 6:
    main.func·001()
    /home/dfc/src/rr.go:17 +0xcf

    Goroutine 6 (running) created at:
    main.main()
    /home/dfc/src/rr.go:22 +0x174
    ==================
    11859
    Found 1 data race(s)
    exit status 66

    On Thursday, 1 May 2014 07:42:24 UTC+10, Jens Frederich wrote:

    Hi all,

    I've done a little ticker precision test. I want to do something in a 4
    Mhz interval. My test creates a ticker with 250 nsec duration. So I've
    expected 4 * 10^6 / 250 nsec = 16000 calls, but I got no more then 13000.
    What's wrong?

    package main

    import (
    "fmt"
    "time"
    )

    func main() {
    t := time.NewTicker(250 * time.Nanosecond)
    stop := make(chan bool, 1)
    c := 0

    go func() {
    for {
    select {
    case <- t.C:
    c++
    case <- stop:
    return
    }
    }
    }()
    time.Sleep(1*time.Second)
    stop <- true
    fmt.Println(c)
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jens Frederich at Apr 30, 2014 at 10:31 pm
    You're right. That's the correct version.

    func main() {
    t := time.NewTicker(250 * time.Nanosecond)
    stop := make(chan bool, 1)
    out := make(chan int, 1)
    go func() {
    c := 0
    for {
    select {
    case <- t.C:
    c++
    case <- stop:
    out <- c
    return
    }
    }
    }()
    time.Sleep(1*time.Second)
    stop <- true
    fmt.Println(<-out)
    }

    On Wednesday, April 30, 2014 11:55:48 PM UTC+2, Dave Cheney wrote:

    It is worth nothing that this program has a data race.

    lucky(~/src) % go run -race rr.go
    ==================
    WARNING: DATA RACE
    Read by main goroutine:
    main.main()
    /home/dfc/src/rr.go:25 +0x1c8

    Previous write by goroutine 6:
    main.func·001()
    /home/dfc/src/rr.go:17 +0xcf

    Goroutine 6 (running) created at:
    main.main()
    /home/dfc/src/rr.go:22 +0x174
    ==================
    11859
    Found 1 data race(s)
    exit status 66

    On Thursday, 1 May 2014 07:42:24 UTC+10, Jens Frederich wrote:

    Hi all,

    I've done a little ticker precision test. I want to do something in a 4
    Mhz interval. My test creates a ticker with 250 nsec duration. So I've
    expected 4 * 10^6 / 250 nsec = 16000 calls, but I got no more then 13000.
    What's wrong?

    package main

    import (
    "fmt"
    "time"
    )

    func main() {
    t := time.NewTicker(250 * time.Nanosecond)
    stop := make(chan bool, 1)
    c := 0

    go func() {
    for {
    select {
    case <- t.C:
    c++
    case <- stop:
    return
    }
    }
    }()
    time.Sleep(1*time.Second)
    stop <- true
    fmt.Println(c)
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 30, '14 at 9:42p
activeApr 30, '14 at 10:31p
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase