Use runtime.Gosched() instead of time.Sleep

Its not clear for newbies that using `time.Sleep` internally calls  `runtime.Gosched()`

Its better to use it directly, moreover on my machine it shows x175 improvement in speed (7m requests per second vs 40k).
This commit is contained in:
Leonid Bugaev 2013-06-18 13:23:25 +06:00
parent db5276c751
commit 9b7c3653d6

View File

@ -10,6 +10,7 @@ package main
import "fmt" import "fmt"
import "time" import "time"
import "sync/atomic" import "sync/atomic"
import "runtime"
func main() { func main() {
@ -22,14 +23,14 @@ func main() {
// once a millisecond. // once a millisecond.
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
go func() { go func() {
for { for {
time.Sleep(time.Millisecond)
// To atomically increment the counter we // To atomically increment the counter we
// use `AddUint64`, giving it the memory // use `AddUint64`, giving it the memory
// address of our `ops` counter with the // address of our `ops` counter with the
// `&` syntax. // `&` syntax.
atomic.AddUint64(&ops, 1) atomic.AddUint64(&ops, 1)
runtime.Gosched()
} }
}() }()
} }