When not using goroutine,500 * 100000000 times plus one
// 1m12.2857724s
start := time.Now()
for i := 0; i < 500; i++ {
res := 0
for j := 0; j < 100000000; j++ {
res++
}
}
duration := time.Since(start)
fmt.Println(duration)
When using goroutine, 10 goroutines execute 50 * 100000000 times plus one
// 1m12.0174541s
start := time.Now()
ch := make(chan bool)
for i := 0; i < 10; i++ {
go func(ch chan bool) {
for i := 0; i < 50; i++ {
res := 0
for j := 0; j < 100000000; j++ {
res++
}
}
ch <- true
}(ch)
<- ch
}
duration := time.Since(start)
fmt.Println(duration)
Why use goroutine does not save time