A few remarks: 1/ If you want to compare to node.js, please remove the CPU profiling option from the go program - it comes with some overhead.
2/ The comparison is not really fair. node.js implements a unique event loop (everything runs on the same thread). Go generally schedules the goroutines on multiple threads. Since all the goroutines target the same channels, there is some contention. By default, Go uses a number of threads corresponding to the number of CPU cores, as exposed by the system. If I run your Go program on my machine: export GOMAXPROCS=32 > time ./main finished ./main 3.08s user 0.97s system >>> 40% <<< cpu 10.011 total but: export GOMAXPROCS=1 > time ./main finished ./main 0.98s user 0.19s system >>> 11% <<< cpu 10.008 total So when you run Go on 1 thread, the CPU consumption is divided by 4. Regards, Didier. On Monday, February 6, 2017 at 6:56:41 AM UTC+1, fwan...@gmail.com wrote: > > For compared with nodejs: > if I remove all the serverDone case, only left the ticker. The CPU is 5% > in my book, while nodejs cost 0.5%. > The nodejs code is in the attachment. > > > 在 2017年2月6日星期一 UTC+8上午3:00:15,fwan...@gmail.com写道: >> >> I make a test to see the performance of select, and found the result is >> not good. >> >> I make 1000 SeqQueue objects and run its messageLoop function (which does >> a small piece of work, and is listed as below) in 1000 separate go >> routines. The CPU cost is more than 20%. >> If I make the ticker 1 second, the CPU cost can slow down to about 2%. >> >> With pprof, I see the most top cost are methods related to >> runtime.selectGo, runtime.lock. >> >> Who knows is there anything wrong in my case? >> >> func (this *SeqQueue) messageLoop() { >> var ticker = time.NewTicker(100 * time.Millisecond) >> defer ticker.Stop() >> for { >> select { >> case <-serverDone: >> return >> case <-this.done: >> return >> case <-ticker.C: >> this.tickCounter += 1 >> case message := <-this.messages: >> this.messageCounter += 1 >> _ = message >> } >> } >> } >> >> >> >> -- 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.