This is a creation in Article, where the information may have evolved or changed.
Test code:
Package Main
Import (
"FMT"
"Time"
)
Const (
num = 10000000//test 10 million times send and receive
)
Func Main () {
TestChan2 ()
}
Func TestChan2 () {
ST: = time. Now (). Unixnano ()
c: = make (chan int)
Go func () {
var n int
For n = range C {
}
Fmt. Printf ("task TestChan2 cost%d \ r \ n" (time. Now (). Unixnano ()-st)/int64 (Time.millisecond))
Fmt. Printf ("%d", N)
}()
For I: = 0; i < num; i++ {
C <-I
}
Close (c)//closes the channel after sending, and exits the for Range loop in the process
Sleep for a while and make sure to receive Chan complete
Time. Sleep (3 * time. Second)
}
Execution Result:
Test the data on a Win7 notebook as follows:
2.7 to 2.9 seconds to finish sending and receiving.
The data tested on a Docker virtual machine is as follows:
4.9 seconds to 5.2 seconds to complete
Perform results on a single Linux physical machine
Completed in 3.5-4.1 seconds
Some of Chan's tips are:
http://my.oschina.net/goskyblue/blog/191149