当前位置:网站首页>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
边栏推荐
- Quick start to static class variables
- Easy mock local deployment (you need to experience three times in a crowded time. Li Zao will do the same as me. Love is like a festival mock)
- FTP、ssh远程访问及控制
- Client interns of a large factory share their experience face to face
- Switching power supply design sharing and power supply design skills diagram
- Class loading process of JVM
- js获取本机ip地址
- openlayers 5.0 两种居中方式
- arcMap 发布切片服务
- Coordinate conversion WGS-84 to gcj-02 and gcj-02 to WGS-84
猜你喜欢
I just want to leave a note for myself
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
该买什么设备,Keysight 给你挑好了
Matlab 2019 installation of deep learning toolbox model for googlenet network
binlog2sql 工具安装使用及问题汇总
Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
MySQL syntax collation (3)
The fifth bullet of MySQL learning -- detailed explanation of transaction and its operation characteristics
[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
随机推荐
腾讯云GPU最佳实践-使用jupyter pycharm远程开发训练
8266 obtain 18b20 temperature
Wechat applet part of the mobile phone Preview PDF did not respond
深度学习——特征工程小总结
2022.04.23 (lc_763_divided into letter interval)
Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
openlayers 5.0 两种居中方式
UML类图几种关系的总结
Keysight has chosen what equipment to buy for you
I just want to leave a note for myself
Audio signal processing and coding - 2.5.3 the discrete cosine transform
浅谈c语言指针的强制转换
ArcMap连接 arcgis server
Strange problems in FrameLayout view hierarchy
FTP, SSH Remote Access and control
SSDB基础2
Using bafayun to control the computer
One stop service platform for high-level talents and development of comprehensive service platform system for talents
Thoughts on the optimization of examination papers in the examination system