英文:
slice of channels and concurrent function execution
问题
如何创建通道切片并在切片迭代中并发运行函数double(i)
:
package main
import (
"fmt"
"time"
)
func double(i int) int {
result := 2 * i
fmt.Println(result)
time.Sleep(500000000)
return result
}
func notParallel(arr []int) (outArr []int) {
for _, i := range arr {
outArr = append(outArr, double(i))
}
return
}
// 如何以并行方式执行与notParallel函数相同的操作。
// 对于数组的每个元素,double函数应该并发地进行评估,
// 而不需要等待每个下一个元素进行评估。
func parallel(arr []int) (outArr []int) {
var chans []chan int
for i := 0; i < len(arr); i++ {
chans = append(chans, make(chan int))
}
for counter, number := range arr {
go func(counter, number int) {
chans[counter] <- double(number)
}(counter, number)
}
for _, ch := range chans {
outArr = append(outArr, <-ch)
}
return
}
func main() {
arr := []int{7, 8, 9}
fmt.Printf("%d\n", notParallel(arr))
fmt.Printf("%d\n", parallel(arr))
}
由于函数double(i)
休眠500毫秒,函数notParallel(arr []int)
对于arr []int
的3个元素需要工作1500毫秒,但是函数parallel(arr []int)
将在大约500毫秒内完成。
在我的实现中有一个错误...
panic: runtime error: index out of range
...发生在以下行上...
chans[i] = make(chan int) // i = 0
英文:
How to create slice of channels and run function double(i)
concurrently inside slice iteration:
package main
import (
"fmt"
"time"
)
func double(i int) int {
result := 2 * i
fmt.Println(result)
time.Sleep(500000000)
return result
}
func notParallel(arr []int) (outArr []int) {
for _, i := range arr {
outArr = append(outArr, double(i))
}
return
}
// how to do the same as notParallel func in parallel way.
// For each element of array double func should evaluate concuruntly
// without waiting each next element to eval
func parallel(arr []int) (outArr []int) {
var chans []chan int
for i := 0; i < len(arr); i++ {
chans[i] = make(chan int) // i = 0 : panic: runtime error: index out of range
}
for counter, number := range arr {
go func() {
chans0+网站访问量 <- double(number)
}()
}
return
}
func main() {
arr := []int{7, 8, 9}
fmt.Printf("%d\n", notParallel(arr))
fmt.Printf("%d\n", parallel(arr))
}
As function double(i)
sleeps for 500 ms function notParallel(arr []int)
works for 1500 ms for 3 elements of arr []int
but function parallel(arr []int)
would work about 500 ms.
In my implementation have error...
panic: runtime error: index out of range
... on line ...
chans[i] = make(chan int) // i = 0
答案1
得分: 3
在这种情况下,你不需要使用 chan。
package main
import (
"fmt"
"sync"
"time"
)
func double(i int) int {
result := 2 * i
fmt.Println(result)
time.Sleep(500000000)
return result
}
func notParallel(arr []int) (outArr []int) {
for _, i := range arr {
outArr = append(outArr, double(i))
}
return
}
// 如何以并行方式执行与 notParallel 函数相同的操作。
// 对于数组的每个元素,double 函数应该并发地进行评估,
// 而不需要等待每个下一个元素的评估。
func parallel(arr []int) (outArr []int) {
outArr = make([]int, len(arr))
var wg sync.WaitGroup
for counter, number := range arr {
wg.Add(1)
go func(counter int, number int) {
outArr[counter] = double(number)
wg.Done()
}(counter, number)
}
wg.Wait()
return
}
func main() {
arr := []int{7, 8, 9}
fmt.Printf("%d\n", notParallel(arr))
fmt.Printf("%d\n", parallel(arr))
}
因为并行必须等待所有 goroutine 完成。
我注意到你的代码无法工作,因为你在同一个函数作用域中引用了 counter
和 number
。
英文:
In this case, you don't need to use chan.
package main
import (
"fmt"
"sync"
"time"
)
func double(i int) int {
result := 2 * i
fmt.Println(result)
time.Sleep(500000000)
return result
}
func notParallel(arr []int) (outArr []int) {
for _, i := range arr {
outArr = append(outArr, double(i))
}
return
}
// how to do the same as notParallel func in parallel way.
// For each element of array double func should evaluate concuruntly
// without waiting each next element to eval
func parallel(arr []int) (outArr []int) {
outArr = make([]int, len(arr))
var wg sync.WaitGroup
for counter, number := range arr {
wg.Add(1)
go func(counter int, number int) {
outArr0+网站访问量 = double(number)
wg.Done()
}(counter, number)
}
wg.Wait()
return
}
func main() {
arr := []int{7, 8, 9}
fmt.Printf("%d\n", notParallel(arr))
fmt.Printf("%d\n", parallel(arr))
}
Because parallel must wait all of finish of goroutine(s).
And I notice your code doesn't work because you refer counter
, number
in same function scope.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论