On Sun, Sep 16, 2018 at 10:49 AM Lei Ni <nile...@gmail.com> wrote:
func main() {
        ...
        for {
                select {
                case <-ticker.C:
                        // call my function here
                }
        }
}

Is the above the actual code you're using for the CPU usage "benchmark"? If
so, make the loop

for range <-ticker.C  {
        // call my function here
}

instead.

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

Please show the C++ code to let others know what is meant by "the same",
thanks. My guess is that the C++ code only yields the thread to the kernel
which is indeed cheaper than rescheduling goroutines in a Go program when
one of them blocks or sleeps. If you're observing 10% CPU usage, it means
the scheduler does its job in about 100 microseconds. Maybe it can be
improved but it does not look like suspiciously too much to me -
considering what's happening behind the scenes.

FTR, a 1 msec ticker will make something called 1000 times in a second on a
common system only when almost idle otherwise. IIRC, for example Linux
schedules threads on a 100 msec base by default.

-- 

-j

-- 
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