Hi all,

In my Go program, I need to call a function roughly every millisecond. 
There is no hard real time requirement, so as long as I can call that 
function roughly 1,000 times a second (or let's say every 0.9-1.1 
millisecond) everything is fine. 

I found time.Ticker and time.Sleep are both pretty expensive, probably 
because the scheduler is doing some of its magics in the background. Is 
there any alternative approach that can allow me to achieve the same with 
much less overhead? Many thanks!


The following program is consistently showing 20-25% of %CPU in top. 

package main

import (
  "time"
)

func main() {
  ticker := time.NewTicker(time.Millisecond)
  defer ticker.Stop()
  for {
    select {
    case <-ticker.C:
    // call my function here
    }
  }
}

while the following program is showing 10-15% of %CPU in top

package main

import (
  "time"
)

func main() {
  for {
    time.Sleep(time.Millisecond)
    // call my function here
  }
}

As comparison, the same can be done in C++ with about 1% %CPU in top. 




Cheers,

Lei 

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

Reply via email to