当前位置:网站首页>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
边栏推荐
- MySQL syntax collation (5) -- functions, stored procedures and triggers
- MySQL syntax collation (3)
- JVM的类加载过程
- Modify the font size of hint in editext
- Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
- Raspberry pie 18b20 temperature
- MySQL syntax collation
- Redis core technology and practice 1 - start with building a simple key value database simplekv
- Steps to build a deep learning environment GPU
- Reflection on the performance of some OpenGL operations in the past
猜你喜欢

浅谈c语言指针的强制转换

Wechat applet part of the mobile phone Preview PDF did not respond

Client interns of a large factory share their experience face to face

Class loading process of JVM

Redis optimization series (III) solve common problems after master-slave configuration

ArcMap connecting ArcGIS Server

为何PostgreSQL即将超越SQL Server?

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

8266 obtain 18b20 temperature

FTP, SSH Remote Access and control
随机推荐
SSDB基础1
深度学习——特征工程小总结
Network protocol: SCTP flow control transmission protocol
Pdf reference learning notes
HTTP cache - HTTP authoritative guide Chapter VII
Raspberry pie 18b20 temperature
JVM的类加载过程
c1000k TCP 连接上限测试1
Xlslib use
优先使用组合而不使用继承
Openlayers 5.0 reload the map when the map container size changes
[transfer] summary of new features of js-es6 (one picture)
Installation, use and problem summary of binlog2sql tool
什么是消息队列
Web Security
binlog2sql 工具安装使用及问题汇总
openlayers 5.0 两种居中方式
SSDB foundation 2
[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
浅谈c语言指针的强制转换