Golang-goruntime and channel: Efficient Channel

Source: Internet
Author: User
Tags intel core i5
This is a creation in Article, where the information may have evolved or changed.
                                                                                                     

Golang has two very large features, that is, the goruntime and channel, these two features directly freed developers from concurrent and thread synchronization, so that high concurrency and thread synchronization between the code is very simple to write, and occupy less resources, synchronous transmission efficiency.

In terms of resource occupancy, the goroutine will grow or shrink memory consumption from the initial stack memory consumption of 4096 bytes as needed. Synchronous transmission efficiency, I once in song Hong's "Code of the Future" a concise example (in the Book of code in the end with a semicolon, the current Golang has canceled the source code in the end of the semicolon, by the compiler to add, a line of code containing multiple statements will need a semicolon delimited). The following code makes the appropriate adjustments based on the original code.


Package main  Import (  "FMT"  "Time")  func Chanflow (left, right Chan int, Bufferlen int) {  if Bufferle N <= 0 {left   <-1 + <-right  } else {for   I: = 0; i < Bufferlen; i++ {left    <-1 + <-rig HT   }}  }  func main () {  nruntime: = 100000  Lastchan: = Make (Chan int.)   var left chan int = nil< C15/>right: = Lastchan   begin: = time. Now ()  FMT. PRINTLN ("Begin at:", begin) for  I: = 0; i < nruntime; i++ {left   , right = right, make (chan int)   go Chanflow (left, right, 0)  } right   <-0  Result: = & Lt;-lastchan   End: = time. Now ()  FMT. Println ("End   at:", end, time.) Since (begin))  FMT. PRINTLN (Result)}


The program created 10w 1 unbuffered channel, 10w goruntime, data in Goruntime from the first channel to the last channel, each inflow once the value plus one. The code runs in my Notebook (2.7 GHz Intel Core i5, 8 GB 1867 MHz DDR3) with the following results:
Begin at:2016-08-28 14:42:04.972728029 +0800 CST
End at:2016-08-28 14:42:05.454288408 +0800 CST 481.560725ms
Takes less than half a second.

In the example above, the buffer channel is used, and the code is modified to have a channel with 1000 units buffered and try again, the code is as follows:


Func Chanflow (left, right Chan int, Bufferlen int) {...} func main () {  nruntime: = 100000  Chanbuffer: = +  R Esult: = Make ([]int, 0, +)   Lastchan: = make (chan int, chanbuffer)   var left chan int = nil Right  : = lastchan< C6/>begin: = time. Now ()  FMT. PRINTLN ("Begin at:", begin) for  I: = 0; i < nruntime; i++ {left   , right = right, make (chan int, chanbuffer)   go Chanflow (left, right, Chanbuffer)  } for   I: = 0 ; i < Chanbuffer; i++ {Right   <-0  } for   I: = 0; i < Chanbuffer; i++ {   result = Append (result, <-lastchan) 
   
    }   End: = time. Now ()  FMT. Println ("End   at:", end, time.) Since (begin))  FMT. PRINTLN (Result)}
   


The results of the operation are as follows:
Begin at:2016-08-28 14:54:09.352472708 +0800 CST
End at:2016-08-28 14:54:14.155240335 +0800 CST 4.802767822s
In less than 5 seconds, 1000 data passes through 10w of 1 channel in 10w Goruntime.

And in the actual production, more need to pass the data is a string, then the code again to try to modify, the code is as follows:


Package main import ("Crypto/rand" "Encoding/base64" "FMT" "io" "Time") func Chanflow (left, right Chan string, BU Fferlen int) {if Bufferlen <= 0 {left <-<-right} else {for I: = 0, i < Bufferlen; i++ {left &lt ;-<-right}}} func genstring () string {b: = make ([]byte, +) If _, err: = Io. Readfull (Rand. Reader, B); Err! = Nil {return ""} else {return base64. Urlencoding.encodetostring (b)}} Func main () {nruntime: = 100000 Chanbuffer: = + Result: = Make ([]string, 0, 10 0) Lastchan: = Make (chan string, chanbuffer) Dataforchan: = Do ([]string, 0, Chanbuffer) for I: = 0; i < Chanbuffer; i++ {Dataforchan = append (Dataforchan, genstring ())} var left chan string = nil right: = Lastchan begin: = time. Now () Fmt. PRINTLN ("Begin at:", begin) for I: = 0; i < nruntime; i++ {left, right = right, make (Chan string, chanbuffer) go Chanflow (left, right, Chanbuffer)} for I: = 0; i < Chanbuffer; i++ {Right &Lt;-Dataforchan[i]} for I: = 0; i < Chanbuffer; i++ {result = Append (result, <-lastchan)} end: = time. Now () Fmt. Println ("End at:", end, time.) Since (begin)) FMT. PRINTLN (Result)}



The results of the operation are as follows:

Begin at:2016-08-28 15:06:25.349599328 +0800 CST
End at:2016-08-28 15:06:31.288183546 +0800 CST 5.938584364s
In less than 6 seconds, 1000 x 44-byte random strings crossed 10w 1 channel in 10w Goruntime. The 1w 44-byte random string crosses the 1w 1 channel in 1w Goruntime for about 5 seconds.

As can be seen above, data in the Golang is very efficient in goruntime through channel synchronization.

This article from: CSDN blog

Thanks to the author: changjixiong

View Original: Golang-goruntime and Channel: Efficient Channel




View Original: http://www.zoues.com/2016/10/20/golang-goruntime%e4%b8%8echannel%e9%ab%98%e6%95%88%e7%9a%84channel/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.