当前位置:网站首页>goroutine
goroutine
2022-04-23 19:21:00 【Handsome that handsome】
The original version :
func add(a int) int {
// Assume that processing business logic requires 100ms
time.Sleep(time.Millisecond * 100)
return a
}
func main() {
sum := 0
start := time.Now().UnixNano() / 1e6
for i := 0; i < 100; i++ {
sum += add(i)
}
end := time.Now().UnixNano() / 1e6
fmt.Printf(" Time consuming :%d\n", end-start)
fmt.Println(sum)
}
result :
Time consuming :11012
4950
Modified code
func add1(a int,allocatChan chan int) {
time.Sleep(time.Millisecond * 100)
allocatChan<-a
}
func main() {
var sum int = 0
var allocatChan chan int = make(chan int,100)
var flag int = 0
start := time.Now().UnixNano() / 1e6
for i := 0; i < 100; i++ {
go add1(i,allocatChan)
}
for{
if flag == 100{
break
}
flag+=1
a:=<-allocatChan
sum+=a
}
end := time.Now().UnixNano() / 1e6
fmt.Printf(" Time consuming :%d\n", end-start)
fmt.Println(sum)
}
result :
Time consuming :90
4950
版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557451077.html
边栏推荐
- Xlslib use
- 命令-sudo
- FTP, SSH Remote Access and control
- 2022.04.23 (lc_763_divided into letter interval)
- [report] Microsoft: application of deep learning methods in speech enhancement
- 開關電源設計分享及電源設計技巧圖解
- 5 minutes to achieve wechat cloud applet payment function (including source code)
- Wechat video extraction and receiving file path
- An example of using JNI to directly access surface data
- Some ideas about time-consuming needs assessment
猜你喜欢

First experience of using fluent canvas

ArcMap publishing slicing service

Common SQL commands

Android Development: the client obtains the latest value in the database in real time and displays it on the interface

FTP、ssh远程访问及控制

Partage de la conception de l'alimentation électrique de commutation et illustration des compétences en conception de l'alimentation électrique

5 minutes to achieve wechat cloud applet payment function (including source code)

Network protocol: SCTP flow control transmission protocol

OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!

Matlab 2019 installation of deep learning toolbox model for googlenet network
随机推荐
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
Openlayers 5.0 discrete aggregation points
開關電源設計分享及電源設計技巧圖解
Modify the font size of hint in editext
SSDB Foundation
Codeworks round 783 (Div. 2) d problem solution
MySQL practical skills
Partage de la conception de l'alimentation électrique de commutation et illustration des compétences en conception de l'alimentation électrique
什么是消息队列
深度学习——特征工程小总结
Openharmony open source developer growth plan, looking for new open source forces that change the world!
An idea of rendering pipeline based on FBO
Steps to build a deep learning environment GPU
Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
[记录]TypeError: this.getOptions is not a function
2021-2022-2 ACM集训队每周程序设计竞赛(8)题解
5 minutes to achieve wechat cloud applet payment function (including source code)
高层次人才一站式服务平台开发 人才综合服务平台系统
命令-sudo
openlayers 5.0 两种居中方式